我有这个文件,testpi.txt,我想把它转换成一个句子列表。
>>>cat testpi.txt
This is math π.
That is moth pie.以下是我所做的:
r = open('testpi.txt', 'r')
sentence_List = r.readlines()
print sentence_List 而且,当输出被发送到另一个文本文件output.txt时,它在output.txt中是这样的。
['This is math \xcf\x80. That is moth pie.\n']
我也试过编解码器,r = codecs.open('testpi.txt', 'r',encoding='utf-8'),
但是输出在所有条目中都包含一个领先的“u”。
如何将这个字节字符串- \xcf\x80显示为π,在output.txt中
请引导我,谢谢。
发布于 2016-05-11 08:44:47
问题是,您正在打印整个list,这给您提供了您不需要的输出格式。相反,单独打印每个字符串,它将工作:
r = open('t.txt', 'r')
sentence_List = r.readlines()
for line in sentence_List:
print line,或者:
print "['{}']".format("', '".join(map(str.rstrip, sentence_List)))https://stackoverflow.com/questions/37156175
复制相似问题