We're a community of 1076K IT Pros here for help, advice, solutions, professional growth and fun. Join us!
1,075,931 Members — Technology Publication meets Social Media
Username:
Password:
Lost login information?
Start New Discussion Reply to this Discussion

Python input file, from column to rows

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

I used .split but not getting the same result. I dont know what to do now.

2
Contributors
3
Replies
24 Minutes
Discussion Span
1 Year Ago
Last Updated
4
Views
blindg35
Newbie Poster
2 posts since Feb 2012
Reputation Points: 10
Solved Threads: 0
Skill Endorsements: 0

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

Thanks a lot woooee. I have one question regarding counter, is print_max is for the max counter limit ? I was trying for loops and I was getting wrong answers I guess.

Quoted to include [code] tags.

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.

f = file('cities.txt','r')

print_max=5
ctr=0
while True:
    line = f.readline()
    x = line.strip()
    if len(x) == 0:
       break
#    x= '!'+x+'!'
#    x= x[0:15]
#    x= x.rjust(15)
    print x,     ## note the comma
    ctr += 1
    if ctr >= max:
        print
        ctr = 0
print     ## newline for final print
blindg35
Newbie Poster
2 posts since Feb 2012
Reputation Points: 10
Solved Threads: 0
Skill Endorsements: 0

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

This article has been dead for over three months: Start a new discussion instead

Post: Markdown Syntax: Formatting Help
 
You
View similar articles that have also been tagged:
 
© 2013 DaniWeb® LLC
Page rendered in 0.0643 seconds using 2.65MB