Dominix
Dominix

Reputation: 280

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'

Upvotes: 1

Views: 282

Answers (2)

Sukrit Kalra
Sukrit Kalra

Reputation: 34483

The print statement automatically appends a new line. You don't need to do a print '\n' again.

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".

Upvotes: 4

djc
djc

Reputation: 11711

The print statement, unless the values passed to it end with a comma, always appends an extra newline.

Upvotes: 4

Related Questions