0

I am attempting to open two files then take the first line in the first file, write it to an out file, then take the first line in the second file and append it to the same line in the output file, separated by a tab.

I've attempted to code this, and my outfile just ends up being the whole contents of the first file, followed by the entire contents of the second file. I included print statements just because I wanted to see something going on in the terminal while the script was running, that is why they are there. Any ideas?

import sys


InFileName = sys.argv[1]                 

InFile = open(InFileName, 'r') 

InFileName2 = sys.argv[2]

InFile2 = open(InFileName2, 'r')

OutFileName = "combined_data.txt"

OutFile = open(OutFileName, 'a')

for line in InFile:
    OutFile.write(str(line) + '\t')
    print line
    for line2 in InFile2:
        OutFile.write(str(line2) + '\n')
        print line  

InFile.close()
InFile2.close()
OutFile.close()
0

2 Answers 2

4

You can use zip for this:

with open(file1) as f1,open(file2) as f2,open("combined_data.txt","w") as fout:
     for t in zip(f1,f2):
         fout.write('\t'.join(x.strip() for x in t)+'\n')

In the case where your two files don't have the same number of lines (or if they're REALLY BIG), you could use itertools.izip_longest(f1,f2,fillvalue='')

Sign up to request clarification or add additional context in comments.

4 Comments

Thanks for the help, however, as I explained to the other commenter, I am having a difficult time getting this to work. It seems to take all of the lines of my first file, then appends the first line of the second file to last line of the first. Rather, I was hoping for: "line1file1"tab"line1file2"linebreak "line2file1"tab"line2file2"linebreak etc. Thank you very much for your help!
@Micrasema -- that's exactly what this should do. Can you check it again?
tried it again in python3 and it worked better, but still maintains the carriage returns after the lines from the first file, is there a way to strip these? Cheers
@Micrasema -- Sorry, I see the problem now. I've updated with a version that strips the whitespace off the end of the lines (which I hadn't taken into account before)
0

Perhaps this gives you a few ideas: Adding entries from multiple files in python

o = open('output.txt', 'wb')

fh = open('input.txt', 'rb')
fh2 = open('input2.txt', 'rb')

for line in fh.readlines():
    o.write(line.strip('\r\n') + '\t' + fh2.readline().strip('\r\n') + '\n')

## If you want to write remaining files from input2.txt:
# for line in fh2.readlines():
#     o.write(line.rstrip('\r\n') + '\n')

fh.close()
fh2.close()
o.close()

This will give you:

line1_of_file_1        line1_of_file_2
line2_of_file_1        line2_of_file_2
line3_of_file_1        line3_of_file_2
line4_of_file_1        line4_of_file_2

Where the space in my output example is a [tab] Note: no line ending is appended to the file for obvious reasons.

For this to work, the linendings would need to be proper in both file 1 and 2. To check this:

print 'File 1:'
f = open('input.txt', 'rb')
print [r.read[:200]]
f.close()

print 'File 2:'
f = open('input2.txt', 'rb')
print [r.read[:200]]
f.close()

This should give you something like

File 1:
['This is\ta lot of\t text\r\nWith a few line\r\nendings\r\n']
File 2:
['Give\r\nMe\r\nSome\r\nLove\r\n']

6 Comments

Thanks for the help! Perhaps, I explained it a little bit wrong. I would like to take each line of both files and then add them to the output so that the output looks like: <file1, line1> tab <file2, line1> line break <file1, line2> tab <file2, line2> line break <file1, line3> tab <file2, line3> line break etc. When I run the code, I keep getting every line of the first file then just the first line of the second file appended to the last line of the first file. Sorry, I hope this isn't too tricky.
Ah, i probably missunderstood you. Updated my version of the aproach.
I think it is close. Only the first character in the line of the first file is opened, I think it is iterating a character at a time because each line is opened. The second file appends to the line just fine. I tried changing "for line in fh.readline():" to "for line in fh:", but then the original problem of just appending to the end of the entire first file happened...
forgot an s at the end :P lol! Theres a big difference between fh.readline() and fh.readlines() as ..lines() returns a list of all the lines and readline() takes only one line at the time it's called. So in the example above you would need readlines() :)
thanks for the help. For some reason when I do that, the output goes back to having the entire first file then one line from the second appended to the last line. I thought it might be a problem with python 2.7, so I switched to 3 and I get the dreaded, "TypeError: Type str doesn't support the buffer API" error. Could there be something wrong with my files?
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.