搜索&使用Python在表格中逐行替换

时间:2013-08-22 08:43:49

标签: python-2.7 arcpy arcmap

我使用python为ArcMap编写了一个脚本,该脚本将使用不支持的字符表,Romanize或音译这些适用的字段,并创建一个shapefile,其中包含可能也包含在表中的任何地理信息。我确定的其余代码工作正常。我的主要问题是能够在输入表的每一行中逐字母搜索,我之前已经工作过,但我想我已经恢复了之前的错误。

# Loop through each row in the copied table and replacing each cell with one based on the reference table.
rows = access.SearchCursor(outtable, orfield) # Creates a search cursor that looks in the outtable for what is in the native language field.
row = rows.next() # Establishes a variable that cycles row to row.

while row: # Beginning of "while" loop.
    for r in row: # Searches each letter within the searched row.
        for o in orfield: # Searches each cell based on the searched field from the reference table.
            if r == o: # If/Else statement for seeing if the letter being searched for in the intable (r) is identical to the letter in the orthography field (o).
                r.replace(orfield, profield) # Replaces any identical letters with the associated pronunciation.
            else:
                row.next() # Cycles to the next row.

我觉得我错过了一些东西,但不太确定。如果您需要我详细说明我的脚本中包含的其他内容,请告诉我。不一定需要为我编写脚本,但是如果有我可以使用的模块或功能,请告诉我它是什么以及我可以在哪里阅读它。

1 个答案:

答案 0 :(得分:0)

你的一些细节有点模糊,但似乎代码试图将Field数据对象(用for r in row创建)与某些输入集中的元素进行比较,你似乎暗示它是一个字符串。除了Field vs string的类型不匹配之外,我认为Row对象不能像你编写它那样迭代。您可以使用以下内容获取字段:

fldList = list()
for fld in arcpy.ListFields(r'C:\Workspace\somedata.shp'): 
    fldList.append(fld.name)

然后使用以下内容迭代fldList

for fld in fldList:
     fldValue = row.getValue(fld)
     ....do some regex/string parsing
     ....if condition met, use row.setValue(fld, newValue) and rows.update(row)