0

I am attempting to take information from each file and use it to create a new file. Each file has one line consisting of a series of numbers. I want each line of one file to line up with each line of the other, then take each number in a line of one file to meet the other number in the same position of the other line in the other file. File 'Volume.txt' has each line shifted one point over (hence the code with k = j+1).

*I keep getting a single Chinese character repeated a lot when the operation is done. So where did I go wrong? Thanks a lot!*

Here is the code:

e = open('C:/Users/MC/Desktop/EODP.txt', 'r')
v = open('C:/Users/MC/Desktop/Z/Volume.txt', 'r')
n = open('C:/Users/MC/Desktop/vovere.txt', 'w')

m = 0 #Used later for letting me know that the first line in the new file completed

for i in range(0, 3256):  # there are 3257 lines per the two files EOPD and Volume
    l = []                # create a list to put data in during operation
    er = e.readline()     #
    es = er.split(', ')   # create a list the data from that line of file
    vr = v.readline()     #
    vs = vr.split(', ')   # same
    for  j in range(len(es)):
        k = j + 1         #  file Volume is shifted one point ahead
        try:
            if float(vs[k]) == 0.0:
                vovere = 0.0             # vovere is the name of the output for each point
            elif vs[k] == '' or vs[k] == ' ':
                vovere = 0.0
            else:
                vovere = float(es[j])/float(vs[k])
        except ValueError:         #kept getting this error: some points weren't numbers
            vovere = 0.0       
        except IndexError:         # Each file didn't always exactly equal in line length
            vovere = 0.0
        la = l.append(float(vovere)) #Creates the list for each new line in new file
    ls = str(la)
    l1 = ls.replace('[', '')    # Taking away extra notations so the new file is a little 
    l2 = l1.replace(']', '')    # more clean and can be put into other programs 
    n.write(l2)
    if m == 0:                 # From here on out is just for me, not necessary**
        print("The first line is done!")
        m += 1
    else:
        pass

e.close()                      #** Except these of course
print(e.closed)
print("Yes, EOPD.txt is closed")
v.close()
print(v.closed) 
print("Yes, Volume.txt is closed")
n.close()
print(n.closed) 
print("Yes, vovere.txt is now ready for further manipulation!!")
LJNielsenDk
  • 1,414
  • 1
  • 16
  • 32
mrfc
  • 23
  • 5
  • The question is unclear, is there only one line in each file or is it multiple lines in each file? If there are more than one line in each file, is there more than one number on each line? If there are more numbers on one line, what is the delimiter? I've pretty much skipped reading your code because it seems like it would be easier to write something shorter that would do the job described in the text part of the question. – LJNielsenDk Aug 04 '12 at 16:48
  • 1
    Sorry about the confusion. The two files I'm extracting from have multiple lines each. Each line has multiple data points, each data point is separated by a comma then a space. I'm attempting to match up the 1st line from one file to the 1st line of the the other file, then data point 1 from EODP.txt and data point 2 from Volume.txt, and every other data pointed in both lines, staggering the relative positions. I want this repeated for each subsequent line. Each file has the same number of lines, but not necessisarly the same number of data points per line being used. Hope that helps, thanks! – mrfc Aug 04 '12 at 17:00
  • 1
    Staggering points meaning: file volume, line 1, point 2 with file EODP, line 1, point 1. Then file volume, line 1, point 3 with file EODP, line 1, point 2. Repeat for entire length of file EODP line 1, which should have lenth len(volume line 1)-1. – mrfc Aug 04 '12 at 17:08
  • possible duplicate of [How to merge two files in python](http://stackoverflow.com/questions/9211923/how-to-merge-two-files-in-python) – Paulo Scardine Aug 04 '12 at 18:28
  • There are plenty near duplicates for this question. Just search for "python merge files". – Paulo Scardine Aug 04 '12 at 18:29
  • One problem I can see is `la = l.append(float(vovere))`. The `.append` method _modifies_ the list and then returns `None`, so `la` will be None. – MRAB Aug 04 '12 at 23:44

1 Answers1

0

If I understood the question correctly this should do the trick:

from itertools import zip_longest # If this was python 2.x import izip_longest instead

if __name__ == '__main__':
    input1_lines = []
    input2_lines = []

    # Read all the input from first file
    with open('./input1.txt', 'r') as input1:
        input1_lines = input1.readlines()

    # Read all the input from first file
    with open('./input2.txt', 'r') as input2:
        input2_lines = input2.readlines()

    # Go through all the lines
    output_lines = []
    for line_number_index in range(len(input1_lines)):
    # For each line in each input file make a list of items and remove line breaks
        line1_items = str(input1_lines[line_number_index]).replace('\n', '').split(', ')
        line2_items = str(input2_lines[line_number_index]).replace('\n', '').split(', ')[1:]

        # Go through the item lists and merge them, fill empty spots with fillvalue if there are more items in one of the lists
        this_output_line = []
        for zip_entry in zip_longest(line1_items, line2_items, fillvalue='FILL_VALUE'):
            this_output_line.append(', '.join(zip_entry))
        output_lines.append(', '.join(this_output_line))

    # Write out the file
    with open('./output.txt', 'w') as output_file:
        for line in output_lines:
            output_file.write('%s\n' % line)

Replace FILL_VALUE with whatever you want to insert if there are more items in one of the lines that the other one when merging them.

If you need the first item to be from input2.txt instead of input1.txt, switch line1_items and line2_items around in for zip_entry in zip_longest(line1_items, line2_items, fillvalue='FILL_VALUE'):

input2 is the one where the first item of each line will be ignored.

My test data for this:

input1.txt:

0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10
2, 22, 222, 2222

input2.txt:

z, a, b, c, d, e, f, g, h, i, j, k
z, a, aaa, aaaa

Result:

0, a, 1, b, 2, c, 3, d, 4, e, 5, f, 6, g, 7, h, 8, i, 9, j, 10, k
2, a, 22, aaa, 222, aaaa, 2222, FILL_VALUE
LJNielsenDk
  • 1,414
  • 1
  • 16
  • 32