Reputation: 11
I'm trying to print words in a text file here's my code: shivas_file = open ("words.txt")
Upvotes: 0
Views: 94
Reputation: 598
For better alignment, try this (for Python 3, please change the print call):
shivas_file = ['abc, defggggggggg, kjdfksjdlkfjslkf'
, 'abcfdsfsdfskdjf, gggg, kjdfksjdlkfjslkf'
, 'abc, defggggg, kjdfksjdlkfjslkf']
for line in shivas_file:
x = line.split(",")
s = '%-20s %-20s %-20s' % tuple(x)
print s
output:
abc defggggggggg kjdfksjdlkfjslkf
abcfdsfsdfskdjf gggg kjdfksjdlkfjslkf
abc defggggg kjdfksjdlkfjslkf
Upvotes: 0
Reputation: 1728
you are using syntax of python3 in python2. In python3, print is a function, but in python2 - its just a statement. If you want python2, use
print x[0],'\t',x[1],'\t',x[2]
instead of
print(x[0],'\t',x[1],'\t',x[2])
Upvotes: 0
Reputation: 2567
Use the .format
method.
print '{:<15} {:<15} {:<15}'.format(x[0],x[1]x[2])
Upvotes: 0
Reputation: 8335
try this
x[0]+'\t'+x[1]+'\t'+x[2]
If you don't want tab try this
print (x[0]+x[1]+x[2])
Upvotes: 1
Reputation: 118011
If you're trying to make tab delimited columns
for line in shivas_file:
print('\t'.join(line.split(",")))
Upvotes: 1