Quoted to include
[code]
tags.
Hello guys,
just joined the community. Found it through google. :)
I am basically new to python, only couple of days of knowledge. I am trying to
make all the words in a txt file appear in 4 or 5 in a row.
so the txt file looks like this:
!algiers!
!atlanta!
!baghdad!
!bangkok!
!beijing!
!bogota!
!buenos aires!
!cairo!
!chennai!
!chicago!
!delhi!
!essen!
!ho chi mihn ci
!hong kong!
!istanbul!
!jakarta!
!johannesburg!
and the out put should be:
!algiers! !atlanta! !baghdad! !bangkok!
!beijing! !bogota! !buenos aires! !cairo!
This is the code I used so far
f = file('cities.txt','r')
while True:
line = f.readline()
if len(line) == 0:
break
x= line.strip()
x= '!'+x+'!'
x= x[0:15]
x= x.rjust(15)
print x
Use a counter (code is not tested but for illustration only).
f = file('cities.txt','r')
print_max=5
ctr=0
for line in f:
x = line.strip()
# x= '!'+x+'!'
# x= x[0:15]
# x= x.rjust(15)
print x, ## note the comma
ctr += 1
if ctr >= print_max:
print
ctr = 0
print ## newline for final print
woooee
Posting Maven
2,703 posts since Dec 2006
Reputation Points: 827
Solved Threads: 779
Skill Endorsements: 9
print_max is the maximum number to print on one line. A variable is used so it can be any number input.
woooee
Posting Maven
2,703 posts since Dec 2006
Reputation Points: 827
Solved Threads: 779
Skill Endorsements: 9