Unicode写入正确,读取错误

2024-06-17 12:40:33 发布

您现在位置:Python中文网/ 问答频道 /正文

我有一个包含电影名称和电影数据的.TSV文件,我正在使用PYDOT软件包对其进行分析。该文件链接为Here。包含用于创建它的JSON的文件链接为Here

该文件由解析的JSON编写,并使用utf-8编码编写。虽然文件写得正确,但当我读回Python时,解释器似乎总是停在下面一行:

'Taken\t["Liam Neeson", " Maggie Grace", " Jon Gries", " David Warshofsky"]\n'
'The Walking Dead\t["Andrew Lincoln", " Steven Yeun", " Chandler Riggs",'

输出应如下所示,并在文件中写入:

Taken   ["Liam Neeson", " Maggie Grace", " Jon Gries", " David Warshofsky"]
The Walking Dead    ["Andrew Lincoln", " Steven Yeun", " Chandler Riggs", " Norman Reedus"]
Toy Story 3 ["Tom Hanks", " Tim Allen", " Joan Cusack", " Ned Beatty"]

以下是用于创建文本文件的代码:

step3v2=open('step3.txt', 'rU')
step4=codecs.open('step4.txt', mode='w', encoding='utf-8')
data=[]
merged=''
for line in step3v2:
    data.append(json.loads(line))

for row in data:
    moviename=row[u'Title']
    row[u'Actors']=row[u'Actors'].split(',')
    actors=json.dumps(row[u'Actors']) + '\r\n'
    merged+=moviename + '\t'
    merged+=actors
step4.write(merged)

以下是读取该文件的代码:

graph=pydot.Dot(graph_type='graph', charset='utf8')
step4v2=open('step4.txt', 'rU')


textfile=step4v2.readlines()
for line in textfile:
    print repr(line)

Tags: 文件intxtfordatahere电影链接
1条回答
网友
1楼 · 发布于 2024-06-17 12:40:33
step4v2=open('step4.txt', 'rU') #this means universal newlines

应该是

step4v2=open('step3.txt', 'rb') #this means read the binary data

使用您链接的dropbox上的文件

>>> f =open (os.path.expanduser("~\\Downloads\\step4.txt"),"rb")
>>> for line in f: print repr(line)

看起来效果不错

相关问题 更多 >