我将在下面发布我的代码。我一直在尝试创建一个自动更新脚本,以便在文件地理数据库中创建一个市民地址要素类,并且该脚本除了最后一步外还具有预期的功能:我正在尝试添加一个包括街道名称和街道标题(街道,道路,街等),其基于另一个字段中的“之前或之后”标志(街道名称之前为1,之后名称为2),但是我似乎遇到了Unicode错误。我是python的新手,所以我不熟悉使用不同的Unicode设置。我试过包括:
# -*- coding: utf-8 -*-
作为代码的第一行,但无济于事。我收到的错误如下:
回溯(最近通话最近): 第252行的文件“ P:\ AFT \ Sept2018 \ CADB_update_working \ CADB_update \ CADB_updateScript_test_complete_a_test_a.py” 对于光标中的行: UnicodeDecodeError:“ utf8”编解码器无法解码位置3的字节0xe9:数据意外结束
我完全希望这是我没有发现的一些简单的拼写错误或语法错误,或者是我从txt文件生成的csv中存在一些缺陷。代码中有问题的部分如下:
# uses txt file to write a csv file
txtFile = inFolder + "\\street_types.txt"
csvFile = inFolder + "\\street_types.csv"
with open(txtFile, 'rb') as inFile, open(csvFile, 'wb') as outFile:
in_txt = csv.reader(inFile, delimiter = '\t')
out_csv = csv.writer(outFile)
out_csv.writerows(in_txt)
print "CSV created"
# writes two columns of the csv into 2 lists and then combines them into a dictionary
with open(csvFile,'r') as csvFile:
reader = csv.reader(csvFile, delimiter=',')
next(reader, None)
listA = [] # CD
listB = [] # Display Before Flag
listC = [] # NAME
for row in reader:
listA.append(row[0])
listB.append(row[3])
listC.append(row[1])
# print listA
# print listB
keys = map(int, listA)
values = map(int, listB)
dictionary = dict(zip(keys,values))
print dictionary
keysB = map(int, listA)
valuesB = listC
dictionaryB = dict(zip(keysB,valuesB))
print dictionaryB
# uses that dictionary to update the field just added to teh feature class with the corresponding boolean value
print "Dictionaries made successfully"
update_fields = ["ST_TYPE_CD","ST_NAME_AFTER_TYPE"]
with arcpy.da.UpdateCursor(fc, update_fields) as cursor:
for row in cursor:
if row[0] in dictionary:
row[1] = dictionary[row[0]]
cursor.updateRow(row)
# Adding more fields to hold the concatenated ST_TYPE_CD and STREET_NAME based on ST_NAME_AFTER_TYPE
field_name = "ST_NAME_COMPLETE"
if arcpy.ListFields(fc, field_name):
print "Field to be added already exists"
else:
arcpy.AddField_management(fc, "ST_NAME_COMPLETE", "TEXT")
print "Field added"
field_name = "ST_TYPE"
if arcpy.ListFields(fc, field_name):
print "Field to be added already exists"
else:
arcpy.AddField_management(fc, "ST_TYPE", "TEXT")
print "Field added"
# Populating those added fields
fields = ["ST_TYPE_CD","ST_TYPE","STREET_NAME"]
where = "STREET_NAME IS NOT NULL"
with arcpy.da.UpdateCursor(fc, fields, where) as cursor:
for row in cursor:
if row[0] in dictionaryB:
row[1] = dictionaryB[row[0]]
cursor.updateRow(row)
print "One of two field transcriptions complete"
fields = ["ST_TYPE","STREET_NAME","ST_NAME_COMPLETE","ST_NAME_AFTER_TYPE"]
with arcpy.da.UpdateCursor(fc, fields, where) as cursor:
for row in cursor:
if row[3] == 1:
row[2] = row[0] + " " + row[1]
elif row[3] == 2:
row[2] = row[1] + " " + row[0]
cursor.updateRow(row)
print "Two of two field transcriptions complete"
如果预计会出现csv文件问题,我可以尝试上传该文件或显示其中包含的数据片段。
我已经坚持了一段时间,所以任何帮助或建议都将不胜感激。
答案 0 :(得分:0)
如上所述,该问题的解决方法是更改
listC.append(row[1])
到
listC.append(row[1].decode('cp1252'))
此操作将列表值从字符串转换为Unicode字符串(例如u'string'),这使得随后的过程可以正确解释unicode字符。