The Shed is going Social! Join us on FaceBook and Twitter and chime in on the conversation.
|
 |
|
Dev Shed Forums
> Programming Languages
> Python Programming
|
Writing text file from other text files
Discuss Writing text file from other text files in the Python Programming forum on Dev Shed. Writing text file from other text files Python Programming forum discussing coding techniques, tips and tricks, and Zope related information. Python was designed from the ground up to be a completely object-oriented programming language.
|
|
 |
|
|
|
|

Dev Shed Forums Sponsor:
|
|
|

January 8th, 2013, 02:48 AM
|
|
Registered User
|
|
Join Date: Jan 2013
Posts: 4
Time spent in forums: 38 m 8 sec
Reputation Power: 0
|
|
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.
|

January 8th, 2013, 08:36 AM
|
 |
Contributing User
|
|
|
|
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!
|

January 8th, 2013, 09:20 AM
|
|
Registered User
|
|
Join Date: Jan 2013
Posts: 4
Time spent in forums: 38 m 8 sec
Reputation 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:
how can I use the script you told me?
thanks again 
|

January 8th, 2013, 09:56 AM
|
 |
Contributing User
|
|
|
|
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.)
|

January 8th, 2013, 11:55 AM
|
|
Registered User
|
|
Join Date: Jan 2013
Posts: 4
Time spent in forums: 38 m 8 sec
Reputation Power: 0
|
|
well, it's exactly the output I want.
I don't understand one thing: why in the two first lines you wrote what do you do with this declaration?
|

January 8th, 2013, 12:14 PM
|
 |
Contributing User
|
|
|
|
|
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!
|

January 9th, 2013, 03:05 AM
|
|
Registered User
|
|
Join Date: Jan 2013
Posts: 4
Time spent in forums: 38 m 8 sec
Reputation 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
>>>

|

January 9th, 2013, 05:39 AM
|
 |
Contributing User
|
|
|
|
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.
|
Developer Shed Advertisers and Affiliates
| Thread Tools |
Search this Thread |
|
|
|
| Display Modes |
Rate This Thread |
Linear Mode
|
|
Posting Rules
|
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts
HTML code is Off
|
|
|
|
|