2014-02-11 157 views
0

我有一个.TSV文件的电影名称和电影数据,我正在使用PYDOT软件包进行分析。该文件已链接Here。包含用于创建它的JSON的文件链接到HereUnicode写入正确,读取不正确

该文件是从解析的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) 
+0

解释似乎在下面给arbritarily停止线:意味着什么?有没有错误?或者它只是等待或? –

+0

没有错误。解释者根本没有阅读的字符串更多。为了更加清晰,我将编辑该问题。 – Mike

+0

有时候意味着什么?或总是? –

回答

1
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) 

工作得很好,似乎

+0

发生同样的问题。 – Mike

+0

嗯确定这很奇怪... –

+0

错误,也许我告诉过你错了一个改变... –