Python Programming
 
Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
User Name:
Password:
Remember me

The Shed is going Social! Join us on FaceBook and Twitter and chime in on the conversation.

Go Back   Dev Shed ForumsProgramming LanguagesPython Programming

Reply
Add This Thread To:
  Del.icio.us   Digg   Google   Spurl   Blink   Furl   Simpy   Y! MyWeb 
Thread Tools Search this Thread Rate Thread Display Modes
 
Unread Dev Shed Forums Sponsor:
  #1  
Old June 29th, 2012, 07:05 AM
Sverrath Sverrath is offline
Registered User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Jun 2012
Posts: 1 Sverrath User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 40 m 11 sec
Reputation Power: 0
Line split problem (beginner)

Hello everybody.

I'm not new to programmation, but I'm beginner in python. I have a data file which has the form:

'INPUT FILE'
a1 a2 a3 a4 a5
b1 b2 b3 b4 b5
...

and I want to make it in one column as it reads from left to right and top to bottom like this:

'OUTPUT FILE'
a1
a2
a3
a4
a5
b1
b2
b3
b4
b5
...

I know I have to do something with the function str.split(...), but I'm not sure how to use it.

Anyone can help ??

Thanks in advance.

Reply With Quote
  #2  
Old June 29th, 2012, 11:05 AM
WynnDeezl WynnDeezl is offline
Contributing User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Aug 2011
Posts: 139 WynnDeezl User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 2 Days 3 h 53 m 35 sec
Reputation Power: 2
i don't recommend split in this instance

something like this would prob work:

Code:
Data=io.open(filename, mode='r').readlines()
NewData=open(filename,'w')
for Line in Data :
  for Item in Line :
    NewData.write(Item+'\n')
NewData.close()
Comments on this post
b49P23TIvg disagrees: I test almost all my codes before I post.

Last edited by WynnDeezl : June 29th, 2012 at 12:26 PM.

Reply With Quote
  #3  
Old June 29th, 2012, 02:09 PM
b49P23TIvg's Avatar
b49P23TIvg b49P23TIvg is offline
Contributing User
Dev Shed Loyal (3000 - 3499 posts)
 
Join Date: Aug 2011
Posts: 3,386 b49P23TIvg User rank is Major (30000 - 40000 Reputation Level)b49P23TIvg User rank is Major (30000 - 40000 Reputation Level)b49P23TIvg User rank is Major (30000 - 40000 Reputation Level)b49P23TIvg User rank is Major (30000 - 40000 Reputation Level)b49P23TIvg User rank is Major (30000 - 40000 Reputation Level)b49P23TIvg User rank is Major (30000 - 40000 Reputation Level)b49P23TIvg User rank is Major (30000 - 40000 Reputation Level)b49P23TIvg User rank is Major (30000 - 40000 Reputation Level)b49P23TIvg User rank is Major (30000 - 40000 Reputation Level)b49P23TIvg User rank is Major (30000 - 40000 Reputation Level) 
Time spent in forums: 1 Month 2 Weeks 3 Days 14 h 5 m 35 sec
Reputation Power: 383
2 solutions

Code:
# an approach using split.

# the with statement handles errors and closes the file.
with open('in', mode='r') as input_stream:
    Data = input_stream.readlines()

# remove first line if it was really as you say
discard = "'INPUT FILE'"
if Data[0].startswith(discard):
    del Data[0]

with open('out.split', mode='w') as output_stream:
    output_stream.write("'OUTPUT FILE'\n")
    for line in Data:
        output_stream.write('\n'.join(line.split())+'\n')

###33333333333##########################

# More efficient solution.  Replace the spaces with new lines.
with open('in', mode='r') as input_stream:
    Data = input_stream.read()

# remove first line if it was really as you say
discard = "'INPUT FILE'\n"
i = 0
if Data.startswith(discard):
    i = len(discard)

with open('out.replace', mode='w') as output_stream:
    output_stream.write("'OUTPUT FILE'\n")
    output_stream.write(Data[i:].replace(' ','\n'))
__________________
[code]Code tags[/code] are essential for python code!

Reply With Quote
  #4  
Old June 29th, 2012, 05:21 PM
WynnDeezl WynnDeezl is offline
Contributing User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Aug 2011
Posts: 139 WynnDeezl User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 2 Days 3 h 53 m 35 sec
Reputation Power: 2
you're right

it didn't sink in that the split method was required. and you're right, i usually test my code...didn't this time.
Comments on this post
b49P23TIvg disagrees: Laughing aloud. Using your idea "split not required" I came up with second solution I posted.

Reply With Quote
Reply

Viewing: Dev Shed ForumsProgramming LanguagesPython Programming > Line split problem (beginner)

Developer Shed Advertisers and Affiliates



Thread Tools  Search this Thread 
Search this Thread:

Advanced Search
Display Modes  Rate This Thread 
Rate This Thread:


Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
View Your Warnings | New Posts | Latest News | Latest Threads | Shoutbox
Forum Jump

Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
  
 


Powered by: vBulletin Version 3.0.5
Copyright ©2000 - 2013, Jelsoft Enterprises Ltd.

© 2003-2013 by Developer Shed. All rights reserved. DS Cluster - Follow our Sitemap