n0unc3
n0unc3

Reputation: 99

Not concatenating in same line

This is my code:

with open('test1.txt') as f:
    print "printing f"
    print f
    print '**********************'
    for line in f:
        print "printing line each"
        print line
        print '********'
        line2=line.upper()+"abc"
        print "printing line 2"
        print line2
        print '********'
        open('testout.txt','a').write(line2)

And for this I am getting this output:

printing line 2
ROMA
abc

instead of:

printing line 2
ROMAabc

I can't understand what is wrong, can someone help me understand?

P.S: I tried using join method as well, and still got the same result.

I am using python 2.7

Upvotes: 0

Views: 186

Answers (2)

Alireza
Alireza

Reputation: 4516

Just use a .strip(), so

print line.strip().upper()

You're reading a new line separated document. It's got special "\n"s inside.

Upvotes: 1

Ramtin M. Seraj
Ramtin M. Seraj

Reputation: 705

line contains '\n' at the end, you can use this for your goal:

line.strip().upper()

Upvotes: 4

Related Questions