#1
  1. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jun 2012
    Posts
    1
    Rep 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.
  2. #2
  3. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Aug 2011
    Posts
    139
    Rep Power
    3

    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.
  4. #3
  5. Contributing User
    Devshed Demi-God (4500 - 4999 posts)

    Join Date
    Aug 2011
    Posts
    4,709
    Rep Power
    480

    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 and Makefiles!
  6. #4
  7. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Aug 2011
    Posts
    139
    Rep Power
    3

    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.

IMN logo majestic logo threadwatch logo seochat tools logo