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 January 8th, 2013, 02:48 AM
scipio84 scipio84 is offline
Registered User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Jan 2013
Posts: 4 scipio84 User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 38 m 8 sec
Reputation 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.

Reply With Quote
  #2  
Old January 8th, 2013, 08:36 AM
b49P23TIvg's Avatar
b49P23TIvg b49P23TIvg is offline
Contributing User
Dev Shed Loyal (3000 - 3499 posts)
 
Join Date: Aug 2011
Posts: 3,364 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 10 h 28 m 48 sec
Reputation Power: 383
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!

Reply With Quote
  #3  
Old January 8th, 2013, 09:20 AM
scipio84 scipio84 is offline
Registered User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Jan 2013
Posts: 4 scipio84 User rank is Just a Lowly Private (1 - 20 Reputation Level) 
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:
Code:
4 5 6
4 5 6
4 5 6


how can I use the script you told me?

thanks again

Reply With Quote
  #4  
Old January 8th, 2013, 09:56 AM
b49P23TIvg's Avatar
b49P23TIvg b49P23TIvg is offline
Contributing User
Dev Shed Loyal (3000 - 3499 posts)
 
Join Date: Aug 2011
Posts: 3,364 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 10 h 28 m 48 sec
Reputation Power: 383
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.)

Reply With Quote
  #5  
Old January 8th, 2013, 11:55 AM
scipio84 scipio84 is offline
Registered User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Jan 2013
Posts: 4 scipio84 User rank is Just a Lowly Private (1 - 20 Reputation Level) 
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
Code:
python p.py
what do you do with this declaration?

Reply With Quote
  #6  
Old January 8th, 2013, 12:14 PM
b49P23TIvg's Avatar
b49P23TIvg b49P23TIvg is offline
Contributing User
Dev Shed Loyal (3000 - 3499 posts)
 
Join Date: Aug 2011
Posts: 3,364 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 10 h 28 m 48 sec
Reputation Power: 383
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!

Reply With Quote
  #7  
Old January 9th, 2013, 03:05 AM
scipio84 scipio84 is offline
Registered User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Jan 2013
Posts: 4 scipio84 User rank is Just a Lowly Private (1 - 20 Reputation Level) 
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 
>>> 



Reply With Quote
  #8  
Old January 9th, 2013, 05:39 AM
b49P23TIvg's Avatar
b49P23TIvg b49P23TIvg is offline
Contributing User
Dev Shed Loyal (3000 - 3499 posts)
 
Join Date: Aug 2011
Posts: 3,364 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 10 h 28 m 48 sec
Reputation Power: 383
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.

Reply With Quote
Reply

Viewing: Dev Shed ForumsProgramming LanguagesPython Programming > Writing text file from other text files

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