[英]Two new lines instead of one
I banged my head as to why this code is inserting two new lines instead of one. 我为这个代码为什么要插入两行而不是一行而大吃一惊。 Can someone help?
有人可以帮忙吗?
file=open('16052013')
for line in file:
line=line.strip()
splitLine=line.split("\t")
strSentence=splitLine[2]
caseId=splitLine[0]
for word in strSentence.split():
word=word.strip()
print caseId,'\t',word
print '\n'
除非传递给它的值以逗号结尾,否则print语句始终会附加一个换行符。
The print
statement automatically appends a new line. print
语句自动添加新行。 You don't need to do a print '\\n'
again. 您无需再次
print '\\n'
。
Also, it would be better if you used with open('fileName') as f:
in your programs instead of file = open('fileName')
: in that way the file is closed as soon as you exit from the scope of the with
statement and you avoid to shadow the builtin name "file". 另外,最好在程序中将
with open('fileName') as f:
,而不是file = open('fileName')
:这样,一旦退出该范围,文件即被关闭。 with
语句,可以避免隐藏内置名称“文件”。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.