0

I'm trying to print words in a text file here's my code: shivas_file = open ("words.txt")

Shiva3458
  • 11
  • 4

5 Answers5

1

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])
The6thSense
  • 8,103
  • 8
  • 31
  • 65
1

If you're trying to make tab delimited columns

for line in shivas_file:
    print('\t'.join(line.split(",")))
Cory Kramer
  • 114,268
  • 16
  • 167
  • 218
0

Use the .format method.

print '{:<15} {:<15} {:<15}'.format(x[0],x[1]x[2])
letsc
  • 2,515
  • 5
  • 35
  • 54
0

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])
Nikolai Saiko
  • 1,679
  • 26
  • 27
0

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
Mihai Hangiu
  • 588
  • 4
  • 13