如何使用searchcursort从表中提取某些值

2024-06-16 10:10:40 发布

您现在位置:Python中文网/ 问答频道 /正文

我知道以前也有人问过类似的问题,但由于我对Python还不熟悉,所以我没能把其他的讨论和建议应用到我的案例中去!在

我试图编写一个python脚本来从表中提取某些值:我所指的表是不同水深的硝酸盐值的大集合,这些值存储在表的列中。因为我只需要曲面和最深点的值,所以我想搜索行并提取最后一个不是0的值。我已经开始用SearchCursor工具编写一个脚本,但是在这里我想让它搜索第一个0值,然后返回并在前面打印列中的值。。。有人知道如何解决这个问题吗?在

import arcpy

# Set the Workspace
arcpy.env.workspace = "D:\Teresa\Kerstin\SouthernOcean\03_workspace\Teresa"

# Make table
table = "C:/Users/theidema/Desktop/OxzUti_GridP_Annual.csv"

# Create the search cursor
cursor = arcpy.SearchCursor(Table)

# Iterate through the rows
row = cursor.next()
while row:
    print (row.getValue(field))
    row = cursor.next()

Tags: the脚本tablecursorworkspace建议案例next
1条回答
网友
1楼 · 发布于 2024-06-16 10:10:40

一眼望去,我看到了三个错误。在

首先,SearchCursor()参数“Table”是大写的,而变量“Table”不是大写的。它区分大小写,因此它们需要匹配。在

其次,在print语句中使用了“field”作为参数,但是没有定义它。在While循环上方,定义变量如下:field=“NameOfFieldYouWantToSearch”

最后,在While循环中,您不需要重新定义变量“row”。就这样吧光标.下一个(),正在删除“row=”。(循环之前,行=光标.下一个()的计算结果应为True或False,只要有下一行,while循环就会继续运行,如果没有下一行,则将其计算为False,循环终止。)

哦,是的!我注意到你的工作区不是一个文件地理数据库。如果我是你的话,我会在GDB工作。特别是因为功能类不能存在于gdb之外。但是,如果使用shapefile,它可能会起作用。在

import arcpy

# Set the Workspace
arcpy.env.workspace = "D:\Teresa\Kerstin\SouthernOcean\03_workspace\Teresa" 
#If "Teresa" is a GDB, append ".gdb"

# Define variables
table = "C:/Users/theidema/Desktop/OxzUti_GridP_Annual.csv"
field = "" #Add the Field Name that you want to Search

# Call your CSV in SearchCursor
cursor = arcpy.SearchCursor(table)

# Iterate through the rows
row = cursor.next()
while row:
    print (row.getValue(field))
    cursor.next()

相关问题 更多 >