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

    Join Date
    Jan 2013
    Posts
    4
    Rep Power
    0

    Exclamation Writing text file from other text files


    hi to everybody. I read a lot about text writing, but nothing that resolves my interest.

    I have a folder, where I have a serie of .dat files, in which there are data sets.
    I need to take each data set, add some columns of zero at the right side (each data set have different number of rows, but same number of columns) and then, write all data sets together in the same text file .dat, without any kind of parenthesis.

    thanks a lot for your help.
  2. #2
  3. Contributing User
    Devshed Demi-God (4500 - 4999 posts)

    Join Date
    Aug 2011
    Posts
    4,894
    Rep Power
    481
    Code:
    '''
        append a string to each line of stdin, write to stdout
        use: $ python append.py "string" <inFile >outFile
    
        $ ( cd /tmp && echo gulp | python append.py " obnoxiously"  )
        gulp obnoxiously
    '''
    
    import sys
    S = (sys.argv+[''])[1]
    for L in sys.stdin.readlines():
        if L and ('\n' == L[-1]):
            L = L[:-1]
        sys.stdout.write(L+S+'\n')
    [code]Code tags[/code] are essential for python code and Makefiles!
  4. #3
  5. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jan 2013
    Posts
    4
    Rep Power
    0
    well, thanks for your answer, I didn't expected something like this. I understood "readlines" comand works in another way.

    Anyway, I'm a beginner, so I have to ask to help me a little bit more, to right understand.

    If I have a file a.dat in which I find:
    Code:
    1 2 3
    1 2 3
    1 2 3
    1 2 3
    and a file b.dat, in which I find:
    Code:
    4 5 6
    4 5 6
    4 5 6
    how can I use the script you told me?

    thanks again
  6. #4
  7. Contributing User
    Devshed Demi-God (4500 - 4999 posts)

    Join Date
    Aug 2011
    Posts
    4,894
    Rep Power
    481
    Code:
    $ python p.py " 0 0 0"< a.dat > A0
    $ python p.py " 0 0 0"< b.dat > B0
    $ cat A0 B0 
    1 2 3 0 0 0
    1 2 3 0 0 0
    1 2 3 0 0 0
    1 2 3 0 0 0
    4 5 6 0 0 0
    4 5 6 0 0 0
    4 5 6 0 0 0
    $ cat A0 B0 > c.dat
    $ # if you're on a DOS system,  something like copy myfile1.txt+myfile2.txt
    And, if you wanted something completely different please show the example output for the given input.

    Also consider the paste program. (You won't need aix. Install cygwin or there may be a public domain DOS version.)
    [code]Code tags[/code] are essential for python code and Makefiles!
  8. #5
  9. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jan 2013
    Posts
    4
    Rep Power
    0
    well, it's exactly the output I want.
    I don't understand one thing: why in the two first lines you wrote
    Code:
    python p.py
    what do you do with this declaration?
  10. #6
  11. Contributing User
    Devshed Demi-God (4500 - 4999 posts)

    Join Date
    Aug 2011
    Posts
    4,894
    Rep Power
    481
    I almost always run python on the command line. We're not talking the same language without a common understanding of "command shell", and "command".

    `$ ' is my system prompt.

    `python' is the command I want to run.

    `p.py' is the name of my program---I didn't really call it `append.py', I named it `p.py' then forgot to make the changes in all but one of my posts so far in this column. I name most of my python programs for this forum as p.py . Sorry!
    [code]Code tags[/code] are essential for python code and Makefiles!
  12. #7
  13. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jan 2013
    Posts
    4
    Rep Power
    0
    I'm sorry, but I have some problems.

    let's imagine I make a script and I call it text.py. The script would be as follow, for what I understood:

    Code:
    # text.py  
    from matplotlib import *
    from pylab import * 
    from numpy import *
    import os 
    import sys  
    
    append.py " 0 0 0"< a.dat > A0 
    append.py " 0 0 0"< b.dat > B0
    
    cat A0 B0
    
    cat A0 B0 > c.dat
    but, when I import this script in python, this is the result:

    Code:
    comp@linux-li73:~/Desktop/prova> python 
    
    Python 2.7 (r27:82500, Aug 07 2010, 16:54:59) [GCC] on linux2 Type "help", "copyright", "credits" or "license" for more information. 
    
    >>> import text 
    Traceback (most recent call last):   
      File "<stdin>", line 1, in <module>  
      File "text.py", line 9     
         append.py " 0 0 0"< 1.dat > A0
                          ^ 
    SyntaxError: invalid syntax 
    >>>
  14. #8
  15. Contributing User
    Devshed Demi-God (4500 - 4999 posts)

    Join Date
    Aug 2011
    Posts
    4,894
    Rep Power
    481
    Have a friend (or better, the a member of the computer operations staff) who's familiar with unix stand over your shoulder and kibitz while you do as follows.
    Code:
    comp@linux-li73:~/Desktop/prova> PS2=''
    comp@linux-li73:~/Desktop/prova> cat <<EOF >text.py
    import sys
    S = (sys.argv+[''])[1]
    for L in sys.stdin.readlines():
        if L and ('\n' == L[-1]):
            L = L[:-1]
        sys.stdout.write(L+S+'\n')
    EOF
    comp@linux-li73:~/Desktop/prova> python text.py " 0 0 0"< a.dat > A0
    comp@linux-li73:~/Desktop/prova> python text.py " 0 0 0"< b.dat > B0
    comp@linux-li73:~/Desktop/prova> cat A0 B0 | tee c.dat
    1 2 3 0 0 0
    1 2 3 0 0 0
    1 2 3 0 0 0
    1 2 3 0 0 0
    4 5 6 0 0 0
    4 5 6 0 0 0
    4 5 6 0 0 0
    
    comp@linux-li73:~/Desktop/prova> set PS2='> '
    comp@linux-li73:~/Desktop/prova>
    Also please show them this thread so they understand the content of files a.dat and b.dat before you begin this exercise.
    Last edited by b49P23TIvg; January 9th, 2013 at 05:44 AM.
    [code]Code tags[/code] are essential for python code and Makefiles!

IMN logo majestic logo threadwatch logo seochat tools logo