python python中的写行问题

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/1126477/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-11-03 21:32:45  来源:igfitidea点击:

writeline problem in python

python

提问by SilentGhost

I have a very basic problem. I am learning my first steps with python & scripting in general and so even this makes me wonder:

我有一个非常基本的问题。我正在学习使用 python 和脚本编写的第一步,所以即使这让我想知道:

I want to read & write lines to new file:

我想读取和写入新文件的行:

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)

if i print this to the screen i get all my lines done nicely:

如果我将其打印到屏幕上,我会很好地完成所有线条:

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

If i write it to ofile i end up missing some 15 lines:

如果我将它写到 ofile,我最终会丢失一些 15 行:

0,6100,948,8332
0,26100,568,8636
0,11130,555

I would appreciate if someone could point out to me what is it that i don′t understand?

如果有人能指出我不明白的地方,我将不胜感激?

Reg,

注册,

Jaani

贾尼

回答by Tim Hoolihan

You should be calling ofile.close() according to python docs.

您应该根据python docs调用 ofile.close() 。

I'm not sure that writes are fully flushed out to a file without an explicit close.

我不确定写入是否在没有明确关闭的情况下完全刷新到文件中。

Also, as SilentGhost mentioned, check for empty lines in your input file.

另外,正如 SilentGhost 提到的,检查输入文件中的空行。

And as mentioned by stefanw below, that "if.. break" statement isn't necessary in a for in.

正如下面 stefanw 所提到的,在 for in 中不需要“if..break”语句。

回答by SilentGhost

#read following lines which contain data & write it to ofile
for line in ifile:
    if not line:
        continue          #break stops the loop, you should use continue
    ofile.write(line)

回答by stefanw

The "if not line:" - Check is unnecessary.

“if not line:” - 检查是不必要的。

for line in ifile:
    ofile.write(line)

回答by fabrizioM

Why are you using python2.4? the latest is python2.6

你为什么使用python2.4?最新的是python2.6

and then you can use

然后你可以使用

from contextlib import nested
ipath = "C:\Python24\OtsakkeillaSPSS.csv"
opath = "C:\Python24\OtsakkeillaSPSSout.csv"
with nested(open(ipath,'r'), open(opath,'w') as ifile, ofile:

    #read first line with headers
    line1 = ifile.readline()
    print line1

    #read following lines which contain data & write it to ofile
    for line in ifile:
        ofile.write(line)

回答by fabrizioM

oh, why does it look so foolish, the formation i mean? Give it another go...

哦,怎么看起来这么傻,我是说阵型?再试一次...

hi,

你好,

I have a very basic problem. I am learning my first steps with python & scripting in general and so even this makes me wonder:

我有一个非常基本的问题。我正在学习使用 python 和脚本编写的第一步,所以即使这让我想知道:

I want to read & write lines to new file:

我想读取和写入新文件的行:

ifile=open("C:\Python24\OtsakkeillaSPSS.csv", "r") ofile = open("C:\Python24\OtsakkeillaSPSSout.csv", "w")

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

读取带有标题的第一行 line1 = ifile.readline() 打印 line1 读取包含数据的以下行并将其写入文件

for line in ifile: if not line: break ofile.write(line)

对于 ifile 中的行:如果不是行:break ofile.write(line)

if i print this to the screen i get all my lines done nicely:

如果我将其打印到屏幕上,我会很好地完成所有线条:

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

0,26100,568,8636 0,11130,555,3570 0,57100,77,2405 0,60120,116,1193 0,33540,166,5007 0,95420,3016,030,5007 ,4300,692,3969 0,65610,341,2073 0,1720,0,0 0,78850,228,1515 0,78870,118,1222

If i write it to ofile i end up missing some 15 lines:

如果我将它写到 ofile,我最终会丢失一些 15 行:

0,6100,948,8332 0,26100,568,8636 0,11130,555

0,6100,948,8332 0,26100,568,8636 0,11130,555

I would appreciate if someone could point out to me what is it that i don′t understand?

如果有人能指出我不明白的地方,我将不胜感激?

Reg,

注册,

Jaani

贾尼