So I am trying to create two separate arrays from the same column in python. I need one column to be of the entire column but the last row and I need the other to skip over the first row and contain everything else. here is the script I have right now...
import arcpy
myFile = 'file.csv'
lat1 = 'location.lat'
long1 = 'location.long'
long1list = [row[:-1] for row in arcpy.da.SearchCursor(myFile, long1)]
print long1list
print'done'
long2list = [row[1:] for row in arcpy.da.SearchCursor(myFile, long1)]
print long2list
print'done'
Everytime I run the script however, all I get is...
(), (), (), (), (), (), (), (), (), (), (), (), (), (), (), (), (), (), (), (), (), (), (), (), (), (), (), (), (), (), (), (), (), (), (), (), (), (), (), (), (), (), (), (), (), (), (), (), (), (), (), (), (), (), (), (), (), (), (), (), (), (), (), (), (), (), (), (), (), (), (), (), (), (), (), (), (), (), (), (), (), (), (), (), (), (), (), (), (), (), (), (), (), (), (), (), (), (), (), (), (), (), (), (), (), (), (), (), (), (), (), (), (), (), (), (), (), (), (), (), (), (), (), (), (), (), (), (), (), (), (), (), (), (), (), (), (), ()]
done
Any suggestions on what i'm doing wroong or how to fix this I tried usind append
long2list = [row[0] for row in arcpy.da.SearchCursor(myFile, long1)]
long2listap = long2list.append(row[0])
print long2listap
print'done'
but that didn't work either...
You're using SearchCursor incorrectly both in syntax and the file you're working against. If you are working against a table or feature class in ArcMap, then you can continue to use SearchCursor. This code should get you what you need.
import arcpy
myTable = 'table_name'
# Create lists to store the values in.
list1 = []
list2 = []
fields = ['fieldX'] # Only bring back the fields you need.
with arcpy.da.SearchCursor(myTable, fields) as cursor:
# Read the entire table once.
for row in cursor:
# Add this row's data to the lists.
list1.append(row[0])
list2.append(row[0])
# Remove the first entry from list1.
del list1[0]
# Remove the last entry from list2.
del list2[-1]
print 'done'
If you're working with a pure CSV, then you should just read the CSV directly with vanilla python and use a similar approach for writing the data to lists.