为什么我的 python 程序输出中有多余的空行?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/1001601/
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
Why are there extra blank lines in my python program output?
提问by robintw
I'm not particularly experienced with python, so may be doing something silly below. I have the following program:
我对 python 不是特别有经验,所以可能在下面做一些愚蠢的事情。我有以下程序:
import os
import re
import linecache
LINENUMBER = 2
angles_file = open("d:/UserData/Robin Wilson/AlteredData/ncaveo/16-June/scan1_high/000/angles.txt")
lines = angles_file.readlines()
for line in lines:
splitted_line = line.split(";")
DN = float(linecache.getline(splitted_line[0], LINENUMBER))
Zenith = splitted_line[2]
output_file = open("d:/UserData/Robin Wilson/AlteredData/ncaveo/16-June/scan1_high/000/DNandZenith.txt", "a")
output_file.write("0\t" + str(DN) + "\t" + Zenith + "\n")
#print >> output_file, str(DN) + "\t" + Zenith
#print DN, Zenith
output_file.close()
When I look at the output to the file I get the following:
当我查看文件的输出时,我得到以下信息:
0 105.5 0.0
0 104.125 18.0
0 104.0 36.0
0 104.625 54.0
0 104.25 72.0
0 104.0 90.0
0 104.75 108.0
0 104.125 126.0
0 104.875 144.0
0 104.375 162.0
0 104.125 180.0
Which is the right numbers, it just has blank lines between each line. I've tried and tried to remove them, but I can't seem to. What am I doing wrong?
这是正确的数字,每行之间只有空行。我已经尝试并尝试删除它们,但我似乎不能。我究竟做错了什么?
Robin
罗宾
回答by John Machin
For a GENERAL solution, remove the trailing newline from your INPUT:
对于通用解决方案,请从 INPUT 中删除尾随换行符:
splitted_line = line.rstrip("\n").split(";")
Removing the extraneous newline from your output "works" in this case but it's a kludge.
在这种情况下,从您的输出中删除无关的换行符“有效”,但这是一个麻烦事。
ALSO: (1) it's not a good idea to open your output file in the middle of a loop; do it once, otherwise you are just wasting resources. With a long enough loop, you will run out of file handles and crash (2) It's not a good idea to hard-wire file names like that, especially hidden in the middle of your script; try to make your scripts reusable.
另外:(1)在循环中间打开输出文件不是一个好主意;做一次,否则你只是在浪费资源。如果循环足够长,您将耗尽文件句柄并崩溃 (2) 像这样硬连线文件名不是一个好主意,尤其是隐藏在脚本中间的文件名;尝试使您的脚本可重用。
回答by Andrew Hare
Change this:
改变这个:
output_file.write("0\t" + str(DN) + "\t" + Zenith + "\n")
to this:
对此:
output_file.write("0\t" + str(DN) + "\t" + Zenith)
The Zenith
string already contains the trailing \n
from the original file when you read it in.
读Zenith
入时\n
,该字符串已包含原始文件的尾随。
回答by slovon
Alternative solution (handy if you are processing lines from file) is to strip the whitespace:
替代解决方案(如果您处理文件中的行很方便)是去除空格:
Zenith = Zenith.strip();
回答by ojrac
EDIT:See comments for details, but there's definitely a better way. [:-1]
isn't the best choice, no matter how cool it looks. Use line.rstrip('\n')
instead.
编辑:有关详细信息,请参阅评论,但肯定有更好的方法。[:-1]
不是最好的选择,无论它看起来多么酷。使用line.rstrip('\n')
来代替。
The problem is that, unlike file_text.split('\n')
, file.readlines()
does not remove the \n
from the end of each line of input. My default pattern for parsing lines of text goes like this:
问题是,与 不同file_text.split('\n')
,file.readlines()
不会\n
从每行输入的末尾删除。我解析文本行的默认模式是这样的:
with open(filename) as f:
for line in f.readlines():
parse_line(line[:-1]) # funny face trims the '\n'
回答by eduffy
If you want to make sure there's no whitespace on any of your tokens (not just the first and last), try this:
如果您想确保您的任何令牌(不仅仅是第一个和最后一个)上没有空格,请尝试以下操作:
splitted_line = map (str.strip, line.split (';'))