if row[0] is equal row[1] then row[2] is None - python-2.7

I am writing my very first script in Python 2.7. (And this is my very first question/post here and about Python scripting at all)
Solved/found out almost all problems except one.
I can not figure out how to write the following for arcpy.da.UpdateCursor:
if row[0] is equal row[1] then row[2] is None
Any suggestion is appreciated.
I am expecting a relevant answer to my question.
EDIT: Here is the working code and a solution to my question.
import arcpy
fc = 'C:/DB/yourDB.gdb/yourFeatureClass.shp'
fields = ['field0', 'field1', 'field2']
with arcpy.da.UpdateCursor(fc, fields) as cursor:
for row in cursor:
if (row[0] == row[1]):
row[2] = None
cursor.updateRow(row)
del cursor

Here is the correct code and a solution for my question.
import arcpy
fc = 'C:/DB/PGDB.gdb/Parcel_point.shp'
fields = ['NEWNUMBER', 'NEWNUMBER1', 'NEWNUMBER2']
with arcpy.da.UpdateCursor(fc, fields) as cursor:
for row in cursor:
if (row[0] == row[1]):
row[2] = None
cursor.updateRow(row)
del cursor

Related

Getting a variable from a field using arcpy

I am creating a toolbox tool using a python script that creates maps based on user input. I have created a map template that gets saved and altered with python. I am struggling on how to update some text in text boxes in the layout view using Arcpy. I was able to do it with dynamic text with data driven pages, but I couldn't find any python code to get data driven pages to refresh so I decided to try to update the text with python directly. With data driven pages, the dynamic text was pulling the text from an attribute table. I'm fairly new to python so am struggling with how to pull values from a table to use as part of the text. I am able to update text as long as I have the variable defined somewhere else (not from a table), but the only method I found to pull data from a table was with a search cursor but that returns a list rather than a value so I get an error. The feature classes with the text values I want only have one row in them so it is a list of one. How can I convert that list to a value. I am only including the applicable parts of the script. I also removed the actual paths from the code.
import arcpy
import os
ID = arcpy.GetParameterAsText(1)
city = arcpy.GetParameterAsText(3)
WS = os.path.join('path to gdb', "WS")
dfield = 'name'
datefield = 'date'
cfield = "county"
#Use SearchCursor - these features only have one row, but these are my problem because they are lists
wsname = [row[0] for row in arcpy.da.SearchCursor(WS, dfield)]
wsdate = [row[0] for row in arcpy.da.SearchCursor(WS, datefield)]
county = [row[0] for row in arcpy.da.SearchCursor(overview, cfield)]
#update text
for elm in arcpy.mapping.ListLayoutElements(mxd, "TEXT_ELEMENT"):
elm.text = elm.text.replace('WS',wsname) #this doesn't work because wsname is a list
elm.text = elm.text.replace('City',city) #this works
elm.text = elm.text.replace('text2',"words"+ ID +" -more words") #This works
elm.text = elm.text.replace('Name', county) #this doesn't work because county is a list
elm.text = elm.text.replace('Date',wsdate) #this doesn't work because wsdate is a list
arcpy.RefreshActiveView()
mxd.save()
This code will work when run from a arcgis toobox script tool.
# define the aprx file and the layout in the project
import arcpy
aprx = arcpy.mp.ArcGISProject(r'path\to\the\arcgis\aprxfile.aprx')
aprxLayout = aprx.listLayouts()[0] '''adding the zero index will return the first
layout in the layout list, if there is more than one layout'''
# get the attribute value to use for the layout text element
fieldNames = ['FieldName1', 'FieldName2']
with arcpy.da.SearchCursor(r'path\to.gdb\featureclass', fieldNames) as sc:
for row in sc:
if (row[0]) is not None:
field1Value = (row[0])
if (row[0]) is None:
field1Value = 'Null'
if (row[1]) is not None:
field2Value = (row[0])
if (row[1]) is None:
field2Value = 'Null'
# Assign the attribute value to the layout text element
for textElem in aprxLayout.listElements:
if textElem.name == 'name of layout text element in the element properties':
text.Elem.text = field1Value
if textElem.name == 'name of layout text element in the element properties':
text.Elem.text = field2Value
aprx.saveACopy(r'path/to/folder/projectname')
del aprx
I was able to tweak armedwiththeword's code to come up with this.
import arcpy
mxd = arcpy.mapping.MapDocument(path_to_mxd)
fieldNames = ['name', 'date']
with arcpy.da.SearchCursor(WS, fieldNames) as sc:
for row in sc:
if(row[0]) is not None:
field1Value = (row[0])
if(row[0]) is None:
field1Value = 'Null'
if(row[1]) is not None:
field2Value = (row[1])
if(row[1]) is None:
field2Value = 'Null'
fieldName = ['CTY_NAME']
with arcpy.da.SearchCursor(overview, fieldName) as sc:
for row in sc:
if(row[0]) is not None:
field3Value = (row[0])
if(row[0]) is None:
field3Value = 'Null'
# Assign the attribute value to the layout text element
for textElem in arcpy.mapping.ListLayoutElements(mxd,'TEXT_ELEMENT'):
if textElem.name == 'title':
textElem.text = field1Value + " words"
if textElem.name == 'subtitle':
textElem.text = "WS -0"+ ID + " -more words"
if textElem.name == 'city':
textElem.text = city
if textElem.name == 'county':
textElem.text = field3Value
if textElem.name == 'date':
textElem.text = field2Value

how to import data from .csv file to django sqlite using import.py

i want to retrieve data from csv file but my hardware(RFID) output gives me two different entries of same employee for timein & timeout
WorkNames.csv
In this there are double entries i want only one row for one employee in models.py Employee class
so please help in my METHOD1 by suggesting some code which can combine 2rows into one while transfering data into sqlite
OR
by helping in METHOD2 by suggesting some code to import data from output.csv
METHOD 1: I used import.py to import data. Data is imported to sqlite but double entries
import os
import sys
import csv
from datetime import datetime
project_dir='catalog'
sys.path.append(project_dir)
os.environ['DJANGO_SETTINGS_MODULE']='locallibrary.settings'
import django
django.setup()
from catalog.models import employee1
data=csv.reader(open('locallibrary/WorkNames.csv'),delimiter=',')
i=1
from datetime import datetime
for row in data:
if row[0]== '':
break
else:
Employee1=employee1()
Employee1.Date=row[0]
Employee1.Name=row[1]
Employee1.Number=row[2]
if row[3] =="" or row[3]=='Time IN':
Employee1.Time_IN='00:00:00 AM'
else:
Employee1.Time_IN=str(row[3])
if row[4] =="" or row[4]=='Time OUT':
Employee1.Time_OUT='00:00:00 AM'
else:
Employee1.Time_OUT=str(row[4])
Employee1.save()
**METHOD 2:but to get a timein timeout in row I generated a new file **output.csv
the file is genrated properly but data is not importing from csv to sqlite
import os
import sys
import csv
from datetime import datetime
project_dir='catalog'
sys.path.append(project_dir)
os.environ['DJANGO_SETTINGS_MODULE']='locallibrary.settings'
import django
django.setup()
from catalog.models import employee1
data=csv.reader(open('locallibrary/WorkNames.csv'),delimiter=',')
from datetime import datetime
lines=list(data)
r=5
c=5
for i in range(r):
for j in range(1,5):
if lines[0][j]=="":
break
if lines[i][2] == lines[j][2]:
lines[j][3]=lines[i][3]
r=5
c=5
for i in range(r):
for j in range(1,5):
if lines[i][3]!="" and lines[i][4]=="":
lines[i][3]='00:00:00 AM'
lines[i][4]='00:00:00 AM'
break
writer = csv.writer(open('locallibrary/output.csv', 'w'))
for r in lines:
writer.writerow(r)
open('locallibrary/output.csv').close()
data1=csv.reader(open('locallibrary/WorkNames.csv'),delimiter=',')
i=1
for row in data:
if row['Date']=='':
break
else:
Employee1=employee1()
Employee1.Date=row['Date']
Employee1.Name=row['Name']
Employee1.Number=row['Number']
Employee1.Time_IN=row['Time IN']
Employee1.Time_OUT=row['Time OUT']
Employee1.save()
so please help in my METHOD1 by suggesting some code which can combine 2rows into one while transfering data into sqlite
OR
by helping in METHOD2 by suggesting some code to import data from output.csv
Suggestion for your method 1
If there aren't to many rows in your CSV file (maybe less than 10.000), then you could use a python dict to group by employee number and date.
I modified a little part of your code:
... some code ...
record_dict = {}
for row in data:
if row[0] == '':
break
record_date = row[0]
emp_number = row[2]
key = (emp_number, record_date)
if key not in record_dict:
record_dict[key] = employee1()
record_dict[key].Date = row[0]
record_dict[key].Name = row[1]
record_dict[key].Number = row[2]
record_dict[key].Time_IN = None
record_dict[key].Time_OUT = None
if row[3] not in ('', 'Time IN'):
record_dict[key].Time_IN = row[3]
if row[4] not in ('', 'Time OUT'):
record_dict[key].Time_OUT = row[4]
for emp in record_dict.values():
emp.save()
By the way: you should read PEP8, which is the python coding style guide showing the preferred way to write python code.

Export data as csv is not working in django

I'm currently trying to export a csv file if the print button is clicked.
The problem is the file which generated is not .csv file
However, the file content is retrieve the values which I need (I checked by changing type of file manually. Hereby I attached the result).
Could anyone show me the mistakes? Or is it related to any requirement plugin?
Any and all help is greatly appreciated!
import csv
query_data = search_data(request,request.user.userid)
if (request.method == 'POST'):
if 'csvexport' in request.POST:
data = csv_export(query_data)
return HttpResponse (data,content_type='text/csv')
--
def csv_export (data):
response = HttpResponse(content_type='text/csv')
response['Content-Disposition'] = 'attachment; filename="file.csv"'
writer = csv.writer(response)
response.write('\ufeff'.encode('utf8'))
writer.writerow([,'valData'
,'value1'
,'value2'])
for rec in data:
writer.writerow([rec['valData']
,rec['value1']
,rec['value2']])
return response
--
def search_data(request,userid):
cursor = connection.cursor()
query = str("SELECT\n"
" valData,\n"
" MG0.valData as value1,\n"
" MG1.valData as value2,\n"
" FROM\n"
" T_USER AS TU\n"
" LEFT JOIN M_GENERAL AS MG0\n"
" ON MG0.Cd='001'\n"
" LEFT JOIN M_GENERAL AS MG1\n"
" ON MG1.Cd='001'\n")
cursor.execute(query)
row = dictfetchall(cursor,)
return row
Try this,
if (request.method == 'POST'):
if 'csvexport' in request.POST:
return csv_export(query_data)

Django optimize query update

I have a function which opens a txt file (350k lines), reads each line and updates fields in the database. The database has around 250k-300k records.
It runs way too slow for me.
Could you help me with optimizing it the query or the way the import is being done?
Query:
PlaceGroupMatch.objects.filter(**query_kwargs).update(sym_ul=int(line[5]), updated=True, updated_date=datetime.now())
Whole code:
def street_postal_codes():
output_file_name = os.path.join(settings.MEDIA_ROOT, "postal", "ulice_GUS.txt")
with open(output_file_name, 'r+') as handle:
next(handle)
lines = csv.reader(handle, delimiter='\t')
lines = list(lines)
def update_data(lines):
transaction.set_autocommit(False)
for index, line in enumerate(lines):
query_kwargs = {
"sym_ext": int(line[10]),
"sym_pod":int(line[4]),
"updated": False
}
qs = PlaceGroupMatch.objects.filter(**query_kwargs).update(sym_ul=int(line[5]), updated=True, updated_date=datetime.now())
print index
if index % 5000 == 0:
print index, datetime.now()
transaction.commit()
transaction.set_autocommit(True)
update_data(lines)

Not getting any output nor any error for the following code?

i used the following code to scrape data from url(mentioned in the code). I ran the code but its not giving any output nor throwing any error? i am new to python language, it might be a silly problem. Can someone help me?
import csv
import urllib2
import sys
import time
from bs4 import BeautifulSoup
page = urllib2.urlopen('http://www.t-mobile.de/smartphones/0,22727,23392-_3-0--0-all-,00.html').read()
soup = BeautifulSoup(page)
soup.prettify()
with open('TMO_DE_2012-12-26.csv', 'wb') as csvfile:
spamwriter = csv.writer(csvfile, delimiter=',')
spamwriter.writerow(["Date","Month","Day of Week","Device Name","Price"])
items = soup.findAll('div', {"class": "top"},text=True)
prices = soup.findAll('strong', {"class": "preis-block"})
for item, price in zip(items, prices):
textcontent = u' '.join(price.stripped_strings)
print unicode(item.string).encode('utf8').strip()
if textcontent:
spamwriter.writerow([time.strftime("%Y-%m-%d"),time.strftime("%B"),time.strftime("%A") ,unicode(item.string).encode('utf8').strip(),textcontent])
There are no <div class="top"> elements with text on that page, so items is an empty list. Remove the text=True filter:
items = soup.findAll('div', {"class": "top"})
and extract all text from it:
item_text = u' '.join(item.stripped_strings)
if textcontent and item_text:
spamwriter.writerow([time.strftime("%Y-%m-%d"),time.strftime("%B"),time.strftime("%A") , item_text, textcontent])
or, integrated into your existing code:
with open('TMO_DE_2012-12-26.csv', 'wb') as csvfile:
spamwriter = csv.writer(csvfile, delimiter=',')
spamwriter.writerow(["Date","Month","Day of Week","Device Name","Price"])
items = soup.findAll('div', {"class": "top"})
prices = soup.findAll('strong', {"class": "preis-block"})
for item, price in zip(items, prices):
textcontent = u' '.join(price.stripped_strings)
item_text = u' '.join(item.stripped_strings)
if item_text and textcontent:
spamwriter.writerow([time.strftime("%Y-%m-%d"),time.strftime("%B"),time.strftime("%A"), item_text.encode('utf8'),textcontent.encode('utf8')])