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

Closed Thread
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 December 1st, 2012, 12:19 PM
ufloridagator ufloridagator is offline
Registered User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Nov 2012
Posts: 8 ufloridagator User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 1 h 4 m 10 sec
Reputation 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.

Reply With Quote
  #2  
Old December 1st, 2012, 01:27 PM
Dietrich's Avatar
Dietrich Dietrich is offline
Contributing User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Feb 2005
Posts: 499 Dietrich User rank is Second Lieutenant (5000 - 10000 Reputation Level)Dietrich User rank is Second Lieutenant (5000 - 10000 Reputation Level)Dietrich User rank is Second Lieutenant (5000 - 10000 Reputation Level)Dietrich User rank is Second Lieutenant (5000 - 10000 Reputation Level)Dietrich User rank is Second Lieutenant (5000 - 10000 Reputation Level)Dietrich User rank is Second Lieutenant (5000 - 10000 Reputation Level)Dietrich User rank is Second Lieutenant (5000 - 10000 Reputation Level) 
Time spent in forums: 4 Days 3 h 19 m 2 sec
Reputation Power: 63
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

Reply With Quote
  #3  
Old December 1st, 2012, 01:31 PM
b49P23TIvg's Avatar
b49P23TIvg b49P23TIvg is offline
Contributing User
Dev Shed Loyal (3000 - 3499 posts)
 
Join Date: Aug 2011
Posts: 3,458 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 4 Days 6 h 26 m 43 sec
Reputation Power: 403
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!

Reply With Quote
  #4  
Old December 1st, 2012, 02:27 PM
ufloridagator ufloridagator is offline
Registered User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Nov 2012
Posts: 8 ufloridagator User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 1 h 4 m 10 sec
Reputation 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.

Reply With Quote
  #5  
Old December 1st, 2012, 02:36 PM
b49P23TIvg's Avatar
b49P23TIvg b49P23TIvg is offline
Contributing User
Dev Shed Loyal (3000 - 3499 posts)
 
Join Date: Aug 2011
Posts: 3,458 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 4 Days 6 h 26 m 43 sec
Reputation Power: 403
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.

Reply With Quote
  #6  
Old December 1st, 2012, 02:55 PM
ufloridagator ufloridagator is offline
Registered User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Nov 2012
Posts: 8 ufloridagator User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 1 h 4 m 10 sec
Reputation 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.

Reply With Quote
  #7  
Old December 1st, 2012, 03:08 PM
ufloridagator ufloridagator is offline
Registered User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Nov 2012
Posts: 8 ufloridagator User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 1 h 4 m 10 sec
Reputation 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

Reply With Quote
  #8  
Old December 1st, 2012, 03:38 PM
b49P23TIvg's Avatar
b49P23TIvg b49P23TIvg is offline
Contributing User
Dev Shed Loyal (3000 - 3499 posts)
 
Join Date: Aug 2011
Posts: 3,458 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 4 Days 6 h 26 m 43 sec
Reputation Power: 403
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]))

Reply With Quote
  #9  
Old December 1st, 2012, 08:00 PM
ufloridagator ufloridagator is offline
Registered User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Nov 2012
Posts: 8 ufloridagator User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 1 h 4 m 10 sec
Reputation 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
.
.
.
.
.

Reply With Quote
  #10  
Old December 1st, 2012, 09:56 PM
ufloridagator ufloridagator is offline
Registered User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Nov 2012
Posts: 8 ufloridagator User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 1 h 4 m 10 sec
Reputation 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")

Reply With Quote
Closed Thread

Viewing: Dev Shed ForumsProgramming LanguagesPython Programming > Python - read - rewrite on a text file (Driving me crazy)

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