我在编写 python 脚本方面经验并不丰富,但是我正在尝试使用搜索光标调整我在网上找到的代码,以使用 fgdb 中的字段映射表附加数据。我一直收到我认为包含 ASCII 字符 (ÔÇì) 的最后一行代码的语法错误,这是我以前从未见过的。我正在使用 python 2.7.16 并从命令行运行。我已经通过在线语法检查器运行了我的代码,它似乎很好。
任何建议将不胜感激!
错误:
File "C:\Data\ArcGIS\Biodiversity\Activity_Data\scripts_toolboxes\mapFields_Cursor.py", line 55 main()ÔÇìÔÇìÔÇìÔÇìÔÇìÔÇìÔÇìÔÇìÔÇìÔÇìÔÇìÔÇìÔÇìÔÇìÔÇìÔÇìÔÇìÔÇìÔÇìÔÇìÔÇìÔÇìÔÇìÔÇìÔÇìÔÇìÔÇìÔÇìÔÇìÔÇìÔÇìÔÇìÔÇìÔÇìÔÇìÔÇìÔÇìÔÇìÔÇìÔÇìÔÇìÔÇìÔÇìÔÇìÔÇìÔÇìÔÇìÔÇìÔÇìÔÇìÔÇìÔÇì ^ SyntaxError: invalid syntax
我的代码:
# coding: utf-8
# field mapping using Search and insert cursor instead of arcpy.Append
def main():
import arcpy, os
# if you already have the output featureclass:
fc_destination = r'C:\Data\Scripts\Test_scripts\FieldMapping\data.gdb\output_data'
tbl_mapping = r'C:\Data\Scripts\Test_scripts\FieldMapping\data.gdb\fieldMapping'
flds_mapping = [tbl.name for tbl in arcpy.ListFields(tbl_mapping)[1:]]
# [u'DestinationField', u'ROW_Oregon', u'ROW_Arizona', u'ROW_Wyoming', u'ROW_Nevada']
# append featureclass in same order as the fields of the field mapping
lst_fcs = ['path to fc Oregon','path to fc Arizona','path to fc Wyoming','path to fc Nevada']
# create a dictionary with the "field mappings"
dct = {}
with arcpy.da.SearchCursor(tbl_mapping, flds_mapping) as curs:
for row in curs:
lst = list(row)
key = lst.pop(0)
dct[key] = lst
# The dct will have the following content:
# {u'EFFECTIVE_DATE': [u'ROW_DT', u'EFFECTV_DATE', u'ROW_DATE', u'date'],
# u'ROW_TYPE': [u'ROW_TYPE', u'ROW_TP', u'row_tp_txt', u'type'],
# u'GIS_ACRES': [u'ACRES', u'GIS_ACRES', u'AREA_ACRES', u'acre'],
# u'ROW_NAME': [u'ROW_NM', u'NAME', u'row_nm_txt', u'name']}
flds_destination = dct.keys()
flds_destination.insert(0, 'SHAPE@')
# [u'SHAPE@', u'EFFECTIVE_DATE', u'ROW_TYPE', u'GIS_ACRES', u'ROW_NAME']
# insert cursor
with arcpy.da.InsertCursor(fc_destination, flds_destination) as curs:
i=-1
for fc in lst_fcs:
i =+ 1
flds_in = getFieldsIn(flds_destination, dct, i)
# search cursors to insert the features
with arcpy.da.SearchCursor(fc, flds_in) as curs_in:
for row_in in curs_in:
curs.insertRow(row_in)
def getFieldsIn(flds_destination, dct, i):
flds_in = []
for fld_dest in flds_destination[1:]:
lst = dct[fld_dest]
flds_in.append(lst[i])
flds_in.insert(0, 'SHAPE@')
return flds_in
if __name__ == '__main__':
main()