【发布时间】:2016-08-21 21:57:08
【问题描述】:
我正在从一个文件中读取一行,然后将该行复制到另一个文件中。
这是读取文件内容的代码
files = [f for f in os.listdir('.') if os.path.isfile(f)]
for file in files:
if file.endswith(".ttl"):
with open(file) as fileTTL:
lines = fileTTL.readlines()
for line in lines:
writeRDFToFile(line)
对于每一行,我调用 writeRDFToFile 函数,即:
def writeRDFToFile(rdf):
f = open('joined.ttl','w')
try:
rdf = rdf.encode('UTF-8')
f.write(rdf) # python will convert \n to os.linesep
except Exception as e:
print "exception happened " + rdf
print e
f.close()
我收到了这个错误:
'ascii' codec can't decode byte 0xc3 in position XXX: ordinal not in range(128)
关于这个值:
Luis_Buñuel Lasse_Hallström
但正如你所见,我已经尝试使用 UTF-8 对其进行编码,那么为什么错误从一开始就是 ascii 呢?
谢谢
【问题讨论】:
-
你说得对,我的评论毫无意义>_>
标签: python-2.7 utf-8 ascii