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

    Join Date
    Nov 2012
    Posts
    8
    Rep Power
    0

    Python - read - rewrite on a text file (Driving me crazy)


    I am new in python, and I like it a lot so far. I would like to do the following task, but I have not been able to do it and it is driving me crazy.

    I have a file1 which looks like this;

    a b

    c d

    e f

    and I file2 that look like this:

    g

    h

    i

    and I file3 that looks like this:

    j

    k

    l

    I would like to write file2 and file3 into file1, so file1 looks like this:

    a b g j

    c d h k

    e f i l

    Basically coping the other files columns next to the current columns on file1. I understand that in order to do that, I have to read all files and then re-write file 1. I tried, but nothing works. Please, anyone show me how this code should looks like. Thank you so much in advance.
  2. #2
  3. Contributing User
    Devshed Novice (500 - 999 posts)

    Join Date
    Feb 2005
    Posts
    618
    Rep Power
    65
    There are lots of ways to skin this beast. Let's say you have come this far:
    Code:
    # read all three files in as strings
    file1_str = '''\
    ab
    
    cd
    
    ef
    '''
    
    file2_str = '''\
    g
    
    h
    
    i
    '''
    
    file3_str = '''\
    j
    
    k
    
    l
    '''
    
    # split each string at whitespace to a list
    list1 = file1_str.split()
    #print(list1)  # test
    list2 = file2_str.split()
    list3 = file3_str.split()
    
    # combine the lists with zip()
    #print(zip(list1, list2, list3))  # test
    
    mystr = ""
    for c1, c2, c3 in zip(list1, list2, list3):
        mystr += c1 + c2 + c3 + '\n' + '\n'
    
    print(mystr)
    
    # now save mystr to a text file
    
    ''' my result >>>
    abgj
    
    cdhk
    
    efil
    
    '''
    Just a concept, the rest is up to you.
    Real Programmers always confuse Christmas and Halloween because Oct31 == Dec25
  4. #3
  5. Contributing User
    Devshed Demi-God (4500 - 4999 posts)

    Join Date
    Aug 2011
    Posts
    4,963
    Rep Power
    481
    What did you find erroneous about my other answer (post 2)? Now you have 3 files instead of 2 files. But so what. Run the lamination program twice.

    Use the laminate program.
    [code]Code tags[/code] are essential for python code and Makefiles!
  6. #4
  7. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Nov 2012
    Posts
    8
    Rep Power
    0
    b49P23TIvg, I look through the answer you gave me before, and I like it. It looks like it should work. However, when I tried the code, it did not work, and I can't fixed. The another problem is that I cant post any web-links since I am a new user. But On my dropbox I have the codes I have made, the input files and the output files. If there is anyway I could send you the links, and you could help me with my problem, I highly appreciated. I am sorry if I am asking too much, but I cant figure out a good way to make this code.
  8. #5
  9. Contributing User
    Devshed Demi-God (4500 - 4999 posts)

    Join Date
    Aug 2011
    Posts
    4,963
    Rep Power
    481
    Dietrich's answer is Great!

    split the file into fields, transpose it with zip,
    transpose the second file with same technique,
    concatenate the two files (appending rows)
    and then transpose the result!

    Awesome.
    [code]Code tags[/code] are essential for python code and Makefiles!
  10. #6
  11. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Nov 2012
    Posts
    8
    Rep Power
    0
    The answer is great. The files I am trying to "combine" are the outputs of another script. However, I would rather do the "combine" every time I generate each output, so I just use one script and not two, like in the way you suggested on my other post (it just did not work).
    In this post I have three files as examples, but could be 2 or 10, it is a variable number, so the script should be able to do them all. So you suggestion in my other post is better, but I could not make it work.
  12. #7
  13. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Nov 2012
    Posts
    8
    Rep Power
    0
    This is the error that shows up in the previous (in my personal opinion better) script you suggested.
    ------
    Traceback (most recent call last):
    File "paula9.py", line 27, in <module>
    insertion = rline[(start+1)+i].split()[1:3]
    IndexError: list index out of range
  14. #8
  15. Contributing User
    Devshed Demi-God (4500 - 4999 posts)

    Join Date
    Aug 2011
    Posts
    4,963
    Rep Power
    481
    Code:
    # this program works for sufficiently nice input files.
    # The input files need to be rectangular in terms of fields,
    # and they need to have the same number of rows.
    
    print('warning!  This program creates files on your system.')
    print('remove these lines if OK.')
    1/0
    
    def transpose(inp,out=None):
        with open(inp) as inf:
            rows = [row.strip() for row in inf.readlines()]
        if out is None:
            out = inp + '.T'
        with open(out,'w') as ouf:
            for fields in zip(*(row.split() for row in rows)): # "1 liner" is nasty
                ouf.write(' '.join(fields)+'\n')
    
    def laminate(out,*inp):
        '''
            >>> laminate('joinedFile','source.1','source.2','source.n')
            >>>
        '''
        for f in inp:
            transpose(f)
        with open(out+'.T','w') as ouf:
            for f in inp:
                with open(f+'.T') as inf:
                    ouf.write(inf.read())
        transpose(out+'.T',out)
    
    if '__main__' == __name__:
        import sys
        if len(sys.argv) < 3:
            print('use: %s input1 [input2 ...] output')
            sys.exit(1)
        laminate(sys.argv[-1],*(sys.argv[1:-1]))
    [code]Code tags[/code] are essential for python code and Makefiles!
  16. #9
  17. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Nov 2012
    Posts
    8
    Rep Power
    0
    Thanks for the help. It still does not work, the input looks like, thanks anyway for the help, I am just going to give up with this.
    4.6295713E-03 1.3888712E-02 2.3147849E-02 3.2406979E-02 4.1666101E-02 5.0925211E-02 6.0184305E-02 6.9443381E-02 7.8702438E-02 8.7961476E-02 9.7220480E-02 1.0647946E-01 1.1573841E-01 1.2499733E-01 1.3425621E-01 1.4351505E-01 1.5277386E-01 1.6203260E-01 1.7129132E-01 1.8054998E-01 1.8980859E-01 1.9906715E-01 2.0832563E-01 2.1758407E-01 2.2684245E-01 2.3610076E-01 2.4535897E-01 2.5461714E-01 2.6387526E-01 2.7313328E-01 2.8239119E-01 2.9164905E-01 3.0090683E-01 3.1016451E-01 3.1942212E-01 3.2867962E-01 3.3793705E-01 3.4719435E-01 3.5645155E-01 3.6570868E-01 3.7496567E-01 3.8422255E-01 3.9347936E-01 4.0273602E-01 4.1199259E-01 4.2124902E-01 4.3050532E-01 4.3976153E-01 4.4901758E-01 4.5827353E-01
    .
    .
    .
    .
    .
  18. #10
  19. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Nov 2012
    Posts
    8
    Rep Power
    0

    Solution


    This ended up been the easiest solution ever:

    import sys
    from itertools import izip

    filenames = sys.argv[2:]

    filetowrite = sys.argv[1]
    newfile = str(filetowrite) + ".txt"
    opennew = open(newfile, "w")

    files = map(open, filenames)

    for lines in izip(*files):
    opennew.write(('\t'.join(i.strip() for i in lines))+"\n")

IMN logo majestic logo threadwatch logo seochat tools logo