Reputation: 359
There's a file with various names on it, each name as /n at the end of it so when I open the file in python it prints each name in a different line
The problem is I have to print 2 names on each line, I'v tried everything but I still can't because of the "/n" at the end of every name, making it change line
for example :
f=open('nomes.txt','r')
lines=f.readlines()
for line in lines:
print line,
prints each name on different line even with the ',' at the end of the print.
help?
Upvotes: 2
Views: 225
Reputation: 5575
readlines()
returns end of the lines in each token.
You need either to remove it or to use split lines:
f=open('nomes.txt','r')
lines = f.read().splitlines()
for line in lines:
print line,
Then you can use enumerate and print a \n when index is mod 2. Example:
for idx, val in enumerate(lines):
if idx % 2 == 0:
print val, #print without \n
else
print val #print with \n
Upvotes: 2