2013-04-12 142 views
0

我显然还是一名初学者,但我创建了一个解析文件,它通过一个文本文件并生成一个我需要的文件。Python脚本可以在命令行中运行,但不能在运行.py文件时运行

物流并不像明显发生的那样重要。

import fileinput; 
for lines in fileinput.FileInput("c:/manhattan.txt", inplace=1):  
    lines = lines.strip();      
    if lines == '': continue; 
    print(lines); 


source = open('c:/manhattan.txt','r'); 
hrt = open('c:/test.hrt','w'); 
currentline = str(source.readline()); 
currentline.lstrip(); 
workingline = '';      
while currentline[0] != " ": 
    if currentline[0].isdigit() == True: 
     if currentline[0:3] == workingline[0:3] and len(workingline)<160: 
      workingline = workingline + currentline[4:7];   
      currentline = str(source.readline()); 
      currentline.lstrip(); 
     else: 
      hrt.write(('\x01' + 'LOC01LOC' + workingline).ljust(401,' ') +'\n'); 
      workingline = currentline[0:7].replace(';','E'); 
      currentline = str(source.readline()); 
      currentline.lstrip(); 
    else: 
     currentline = str(source.readline()); 
     currentline.lstrip(); 

hrt.write(('\x01'+'LOC50US1 A*').ljust(401,' ' +'\n');                                                                                       
hrt.write(('\x02'+'LOCSUNSAT00:0023:5960 60    99990.00 0.00').ljust(401,' ')+'\n');                                                                                        
hrt.write(('\x02'+'US SUNSAT00:0023:5960 60    99990.03 0.03').ljust(401,' ') +'\n'); 
hrt.close(); 
source.close(); 

它工作正常,在Python命令行,但在运行.py文件时,它简化版,写最后三行文件。

任何想法?

+1

有没有错误? –

+0

'其他链接\ n' '讨论\ n' '喜欢这个网站?我们通过PayPal接受捐赠\ n ' ' 回溯(最近最后一次通话): 文件 “”,1号线,在 IndexError:字符串索引超出范围 >>> hrt.write的((' \ x01'+'LOC50US1 A *')。ljust(401,'')+'\ n'); >>> hrt.write(( '\ X02' + 'LOCSUNSAT00:0023:5960 60 99990.00 0.00'。)LJU ST(401, ' ')+' \ N'); >>> hrt.write(( '\ X02' + 'US SUNSAT00:0023:5960 60 99990.03 0.03')LJU ST(401, ' ')+' \ N'); >>> hrt.close(); >>> source.close(); >>> – user2274589

回答

0

您是否在关闭文件之前尝试刷新缓冲区?

hrt.flush() 
+0

我刚刚尝试在关闭之前添加它,但仍然没有运气。 – user2274589

+0

啊,我现在看到你运行它时实际发生错误。 – skeevey

+0

存在错误,但在python命令行中运行时仍然会打印这3行,但在运行.py文件时不会。 – user2274589

相关问题