3 python
我有一个非常基本的问题.我正在学习python和脚本编写的第一步,所以即便如此,我也想知道:
我想读取和写入新文件的行:
ifile=open("C:\Python24\OtsakkeillaSPSS.csv", "r")
ofile = open("C:\Python24\OtsakkeillaSPSSout.csv", "w")
#read first line with headers
line1 = ifile.readline()
print line1
#read following lines which contain data & write it to ofile
for line in ifile:
if not line:
break
ofile.write(line)
Run Code Online (Sandbox Code Playgroud)
如果我将它打印到屏幕上,我可以很好地完成所有线条:
0,26100,568,8636
0,11130,555,**3570
0,57100,77,2405**
0,60120,116,1193
0,33540,166,5007
0,95420,318,2310
0,20320,560,7607
0,4300,692,3969
0,65610,341,2073
0,1720,0,0
0,78850,228,1515
0,78870,118,1222
Run Code Online (Sandbox Code Playgroud)
如果我把它写到ofile我最终错过了15行:
0,6100,948,8332
0,26100,568,8636
0,11130,555
Run Code Online (Sandbox Code Playgroud)
如果有人可以向我指出我不理解的是什么,我将不胜感激?
注册,
Jaani
你应该根据python docs调用ofile.close().
我不确定写入是否完全刷新到没有显式关闭的文件.
另外,正如SilentGhost所提到的,检查输入文件中的空行.
正如下面的stefanw所提到的那样,"if ... break"声明在for中并不是必需的.
| 归档时间: |
|
| 查看次数: |
10798 次 |
| 最近记录: |