2013-08-22 49 views
1

我使用python編寫了一個ArcMap腳本,該腳本將採用不支持字符的表格,對這些適用字段進行羅曼茲化或音譯,並創建一個包含任何地理信息的shapefile也可能包含在表中。我確定的其他代碼工作正常。我的主要問題是能夠在輸入表的每一行中逐字逐字地搜索,這是我之前工作的,但我想我已經恢復到以前的錯誤。使用Python逐行搜索和替換表中的字母

# 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. 

我覺得我錯過了一些東西,但不太確定。如果您需要我詳細說明腳本中包含的內容,請告訴我。不一定需要爲我編寫腳本,但如果有一個模塊或功能,我可以讓你知道它是什麼以及我可以在哪裏讀到它。

回答

0

對你的一些細節有點模糊,但它似乎代碼試圖比較一個Field數據對象(創建w/for r in row)與某些輸入集中的元素,這似乎是一個字符串。除了字段與字符串的類型不匹配之外,我認爲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)