使用 arcpy.da.SearchCursor 制作两个不同的数组
Using arcpy.da.SearchCursor to make two different arrays
所以我试图从 python 中的同一列创建两个单独的数组。我需要一列是整列但最后一行,我需要另一列跳过第一行并包含其他所有内容。这是我现在的脚本...
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'
然而,每次我 运行 脚本时,我得到的只是...
(), (), (), (), (), (), (), (), (), (), (), (), (), (), (), (), (), (), (), (), (), (), (), (), (), (), (), (), (), (), (), (), (), (), (), (), (), (), (), (), (), (), (), (), (), (), (), (), (), (), (), (), (), (), (), (), (), (), (), (), (), (), (), (), (), (), (), (), (), (), (), (), (), (), (), (), (), (), (), (), (), (), (), (), (), (), (), (), (), (), (), (), (), (), (), (), (), (), (), (), (), (), (), (), (), (), (), (), (), (), (), (), (), (), (), (), (), (), (), (), (), (), (), (), (), (), (), (), (), (), (), (), (), (), (), (), (), ()]
done
关于我做错了什么或如何解决这个问题的任何建议我尝试使用追加
long2list = [row[0] for row in arcpy.da.SearchCursor(myFile, long1)]
long2listap = long2list.append(row[0])
print long2listap
print'done'
但这也没有用...
您在语法和正在处理的文件中都错误地使用了 SearchCursor。如果您正在处理 ArcMap 中的 table 或要素 class,则可以继续使用 SearchCursor。此代码应该可以满足您的需求。
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'
如果您使用的是纯 CSV,那么您应该 read the CSV directly with vanilla python 并使用类似的方法将数据写入列表。
所以我试图从 python 中的同一列创建两个单独的数组。我需要一列是整列但最后一行,我需要另一列跳过第一行并包含其他所有内容。这是我现在的脚本...
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'
然而,每次我 运行 脚本时,我得到的只是...
(), (), (), (), (), (), (), (), (), (), (), (), (), (), (), (), (), (), (), (), (), (), (), (), (), (), (), (), (), (), (), (), (), (), (), (), (), (), (), (), (), (), (), (), (), (), (), (), (), (), (), (), (), (), (), (), (), (), (), (), (), (), (), (), (), (), (), (), (), (), (), (), (), (), (), (), (), (), (), (), (), (), (), (), (), (), (), (), (), (), (), (), (), (), (), (), (), (), (), (), (), (), (), (), (), (), (), (), (), (), (), (), (), (), (), (), (), (), (), (), (), (), (), (), (), (), (), (), (), (), (), (), (), (), (), (), (), ()]
done
关于我做错了什么或如何解决这个问题的任何建议我尝试使用追加
long2list = [row[0] for row in arcpy.da.SearchCursor(myFile, long1)]
long2listap = long2list.append(row[0])
print long2listap
print'done'
但这也没有用...
您在语法和正在处理的文件中都错误地使用了 SearchCursor。如果您正在处理 ArcMap 中的 table 或要素 class,则可以继续使用 SearchCursor。此代码应该可以满足您的需求。
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'
如果您使用的是纯 CSV,那么您应该 read the CSV directly with vanilla python 并使用类似的方法将数据写入列表。