Python 2.6,无法升级
脚本旨在从 arcgis 数据库中获取字段,并创建将 oracle 语句插入文本文件以供日后使用。 3000条记录后有7500条记录出错,说是问题所在。
fieldValue = unicode(str(row.getValue(field.name)),'utf-8',errors='ignore')
我似乎已经尝试了 unicode 和编码的每一种变体。我是 python 的新手,真的只需要有经验的人来查看我的代码,看看问题出在哪里。
import arcpy
#Where the GDB Table is located
fc = "D:\GIS Data\eMaps\ALDOT\ALDOT_eMaps_SignInventory.gdb/SignLocation"
fields = arcpy.ListFields(fc)
cursor = arcpy.SearchCursor(fc)
#text file output
outFile = open(r"C:\Users\kkieliszek\Desktop\transfer.text", "w")
#insert values into table billboard.sign_inventory
for row in cursor:
outFile.write("Insert into billboard.sign_inventory() Values (")
for field in fields:
fieldValue = unicode(str(row.getValue(field.name)),'utf-8',errors='ignore')
if row.isNull(field.name) or fieldValue.strip() == "" : #if field is Null or a Empty String print NULL
value = "NULL"
outFile.write('"' + value + '",')
else: #print the value in the field
value = str(row.getValue(field.name))
outFile.write('"' + value + '",')
outFile.write("); \n\n ")
outFile.close() # This closes the text file
错误代码:
Traceback (most recent call last):
File "tablemig.py", line 25, in <module>
fieldValue = unicode(str(row.getValue(field.name)),'utf-8',errors='ignore')
UnicodeEncodeError: 'ascii' codec can't encode character u'\u2019' in position 76: ordinal not in range(128)
请您参考如下方法:
永远不要在 unicode 对象上调用 str():
>>> str(u'\u2019')
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
UnicodeEncodeError: 'ascii' codec can't encode character u'\u2019' in position 0: ordinal not in range(128)
要以 csv 格式写入包含 Unicode 字符串的行,请使用 UnicodeWriter而不是手动格式化字段。它应该一次解决几个问题。
