python – ‘ascii’编解码器无法解码字节0xef的位置
内容导读
互联网集市收集整理的这篇技术教程文章主要介绍了python – ‘ascii’编解码器无法解码字节0xef的位置,小编现在分享给大家,供广大互联网技能从业者学习和参考。文章包含2927字,纯文字阅读大概需要5分钟。
内容图文
我在这一行上遇到了这个令人讨厌的错误:
r += '\n<Placemark><name>'+row[3].encode('utf-8','xmlcharrefreplace')+'</name>' '\n<description>'+desc.encode('utf-8','xmlcharrefreplace')+'</description>\n' '<Point><coordinates>'+row[clat].encode('utf-8','xmlcharrefreplace')+
','+row[clongitude].encode('utf-8','xmlcharrefreplace')+'</coordinates></Point>\n' '<address>'+row[4].encode('utf-8','xmlcharrefreplace')+'</address>\n' '<styleUrl>'+row[cstyleID].encode('utf-8','xmlcharrefreplace')+'</styleUrl>\n' '</Placemark>'
这是错误:
Traceback (most recent call last):
File "<pyshell#38>", line 1, in <module>
doStuff()
File "C:\Python27\work\GenerateKML.py", line 5, in doStuff
createFiles('together.csv')
File "C:\Python27\work\GenerateKML.py", line 55, in createFiles
'<styleUrl>'+row[cstyleID].encode('utf-8','xmlcharrefreplace')+'</styleUrl>\n' UnicodeDecodeError: 'ascii' codec can't decode byte 0xef in position 60: ordinal not in range(128)
我究竟做错了什么?
谢谢您的帮助.
这里是完整的来源:
import hashlib
import csv
def doStuff():
createFiles('together.csv')
def readFile(fileName):
a=open(fileName)
fileContents=a.read()
a.close()
return fileContents
def readCSVFile(fileName):
return list(csv.reader(open(fileName, 'rb'), delimiter=',', quotechar='"'))
def GetDistinctValues(theFile, theColumn):
with open(theFile, "rb") as fp:
reader = csv.reader(fp)
return list(set(line[theColumn] for line in reader))
def createFiles(inputFile):
cNAME=3
clat=0
clongitude=1
caddress1=4
caddress2=5
cplace=6
ccity=7
cstate=8
czip=9
cphone=10
cwebsite=11
cstyleID=18
inputFileText=readCSVFile(inputFile)
headerFile = readFile('header.txt')
footerFile = readFile('footer.txt')
r=headerFile
DISTINCTCOLUMN=12
dValues = GetDistinctValues(inputFile,DISTINCTCOLUMN)
counter=0
for uniqueValue in dValues:
counter+=1
print uniqueValue
theHash=hashlib.sha224(uniqueValue).hexdigest()
for row in inputFileText:
if uniqueValue==row[DISTINCTCOLUMN]:
for eachElement in row:
eachElement=eachElement.replace('&','&')
desc = ' '.join(row[3:])
r += '\n<Placemark><name>'+row[3].encode('utf-8','xmlcharrefreplace')+'</name>' '\n<description>'+desc.encode('utf-8','xmlcharrefreplace')+'</description>\n' '<Point><coordinates>'+row[clat].encode('utf-8','xmlcharrefreplace')+
','+row[clongitude].encode('utf-8','xmlcharrefreplace')+'</coordinates></Point>\n' '<address>'+row[4].encode('utf-8','xmlcharrefreplace')+'</address>\n' '<styleUrl>'+row[cstyleID].encode('utf-8','xmlcharrefreplace')+'</styleUrl>\n' '</Placemark>'
r += footerFile
f = open(theHash+'.kml','w')
f.write(r)
f.close()
r=headerFile
解决方法:
您可能正在尝试编码字节.在这种情况下,Python首先使用默认编码(ASCII)对字节进行解码,然后使用您提供的编码对结果Unicode字符串进行编码.
解决方案是:不编码字节,即仅对Unicode字符串使用encode().在你的情况下,根本不要使用它.
要创建有效的XML文档,您可以使用xml.etree.ElementTree module.
内容总结
以上是互联网集市为您收集整理的python – ‘ascii’编解码器无法解码字节0xef的位置全部内容,希望文章能够帮你解决python – ‘ascii’编解码器无法解码字节0xef的位置所遇到的程序开发问题。 如果觉得互联网集市技术教程内容还不错,欢迎将互联网集市网站推荐给程序员好友。
内容备注
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 gblab@vip.qq.com 举报,一经查实,本站将立刻删除。
内容手机端
扫描二维码推送至手机访问。