Merge text files

A forum for general discussion of the Python programming language.

Merge text files

Postby Nevenus » Thu Apr 10, 2014 9:40 am

Guys i have delicate problem and I am still learning python, so any help would be mostly welcome.

I have in one file words in 2 columns and 2 words in row represent interaction.
Code: Select all
A0AV96  P25515     
A6H8V1  A4D1U5     
A8YXX4  A6NCZ6     
B0ZBE0  A8BBF9     
B3KWQ6  B3KRK5     
B4E398  A4D1N9     
B6ZGU1  B3KPR3

In second file i have litle bit more information about interactors like this. Here information in every row is extendend info about word from above file.
Code: Select all
A0AV96 RBM47_HUMAN
P25515 E2F8_HUMAN
A0JLT2 MED19_HUMAN
A1ZBR5 AKTP2_DROME
A1ZBT5 MED8_DROME
A2A3L6 TTC24_HUMAN

What i need is some way to merge or find and replace words from this two files that i get from this
Code: Select all
A0AV96  P25515
to this
Code: Select all
A0AV96 RBM47_HUMAN P25515 E2F8_HUMAN


Also i find this code (I DID NOT WRITE IT!) for this example.
“cdf1″
Code: Select all
0 0.0009765625
3 0.011474609375
18 0.013916015625
20 0.014404296875
21 0.091552734375
131 0.092041015625
239 0.162841796875
604 0.164306640625
605 0.166748046875
607 0.167724609375

“cdf2″
Code: Select all
5 0.011474609375
10 0.013916015625
20 0.014404296875
21 0.091552734375
131 0.092041015625
239 0.162841796875
604 0.164306640625
605 0.166748046875
607 0.167724609375
946 0.168212890625

Both files contain two columns. I want to merge them into the following single file:
Code: Select all
0 0.0009765625 5 0.011474609375
3 0.011474609375 10 0.013916015625
18 0.013916015625 20 0.014404296875
20 0.014404296875 21 0.091552734375
21 0.091552734375 131 0.092041015625
131 0.092041015625 239 0.162841796875
239 0.162841796875 604 0.164306640625
604 0.164306640625 605 0.166748046875
605 0.166748046875 607 0.167724609375
607 0.167724609375 946 0.168212890625

Code: Select all
#!/usr/bin/env python
import os, sys, string

if len(sys.argv)<=2:
    print "USAGE: ./cdf-merger.py outfile cdf1 cdf2 …";
    exit(0)

outfile=sys.argv[1]
fdout=open(outfile,"w")

if len(sys.argv)==3:
    cdf1=sys.argv[2]
    os.system("cp  "+cdf1+" "+outfile)
    exit(0)

n_cdfs=len(sys.argv)-2
fnames=[]
fds=[]
curstr=[]
for i in range(2, 2+n_cdfs):
    fnames.append(sys.argv[i])
    fds.append(open(sys.argv[i], "r"))
    curstr.append("NULL")

for i in range(0, 4097): #the final file will contain no more than 4097 lines
    count=0
    for f in range(len(fds)): #read one line from each file
        tmpline = fds[f].readline().rstrip("\n")
        if len(tmpline)>0:
            curstr[f]=tmpline
        else:
            count+=1
    if count==n_cdfs:
        break #all input files have reached their ends
    for f in range(len(fds)):
        if f>0:
            fdout.write(" ")
        fdout.write(curstr[f])
    fdout.write("\n")
fdout.close()

Guys can u help me to litle adapt this code or eaven write new to help me with my problem.

Thanks!
Last edited by Mekire on Thu Apr 10, 2014 11:15 am, edited 1 time in total.
Reason: Wow. First post lock. Code tags added.
Nevenus
 
Posts: 3
Joined: Thu Apr 10, 2014 9:33 am

Re: Merge text files

Postby stranac » Thu Apr 10, 2014 12:25 pm

Here are some instructions for getting help(choose the one that applies to you the best):
  • I am a beginner learning python
    • I have tried writing some code, but it's not working ==> show us what you tried, errors if you get them, and tell us what the problem is
    • I haven't tried anything
      • if you know enough python to try writing something, give it a try
      • if you don't know where to start, tell us how much python you know, and we'll help you find the information you need
  • I have no interest in learning python ==> just let me know, and I'll move it to the Jobs section. Most people around here don't like working for free
Friendship is magic!

R.I.P. Tracy M. You will be missed.
User avatar
stranac
 
Posts: 1143
Joined: Thu Feb 07, 2013 3:42 pm

Re: Merge text files

Postby Nevenus » Thu Apr 10, 2014 5:06 pm

Sorry for I am rude. In profession I am biologist so i only know bare essentials of python, and I plan to learn just right now not too much time. This problem i have is crucial for my scientific research, so if u can help me and it use a little of your time i would be very grateful. Later i will try to learn for myself.
Hvala.
Nevenus
 
Posts: 3
Joined: Thu Apr 10, 2014 9:33 am

Re: Merge text files

Postby metulburr » Thu Apr 10, 2014 7:25 pm

I normally would say the same as stranac, either pay up or attempt to write something. But i am just killing time until a game download is complete.

I am not sure the format in which you wish to use on the first example. It looks just like you are just stripping off the newline from the second file's every other line. The second example though would be something like:
Code: Select all
with open('cdf1.txt') as f1:
    with open('cdf2.txt') as f2:
        f1_lines = f1.readlines()
        f2_lines = f2.readlines()

with open('output.txt', 'w') as fout:
    for i,j in zip(f1_lines, f2_lines):
        formatted_string = '{} {}\n'.format(i.strip(),j.strip())
        fout.write(formatted_string)

the output being the same as the example output you asked for. You can morph this to fit the needs of your first example too.
New Users, Read This
OS Ubuntu 14.04, Arch Linux, Gentoo, Windows 7/8
https://github.com/metulburr
steam
User avatar
metulburr
 
Posts: 1469
Joined: Thu Feb 07, 2013 4:47 pm
Location: Elmira, NY

Re: Merge text files

Postby Nevenus » Fri Apr 11, 2014 12:52 am

i will tryout. Thanks men.
Nevenus
 
Posts: 3
Joined: Thu Apr 10, 2014 9:33 am


Return to General Discussions

Who is online

Users browsing this forum: No registered users and 3 guests