I'm trying to print words in a text file here's my code: shivas_file = open ("words.txt")
Asked
Active
Viewed 83 times
5 Answers
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
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
-
2
-
yes, but I wanted to highlight a possible fact of wrong interpreter choice. It may lead to some troubles later. – Nikolai Saiko Jul 13 '15 at 17:34
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