i have two files containing lists and I would like to perform some manipulations with the numbers.

file 1:

  • A 1

  • B 2

  • C 3

and file 2:

  • A 4

  • B 5

  • C 6

I have written the following code to read in the files and then add list 1 and list 2 together. The code is as follows:

energy_file_in= open('test.dat', "r")
slab_file_in= open('pt3x_slab.dat', "r")
energy_in=energy_file_in.readlines()
slab_in=slab_file_in.readlines()
energy_file_in.close()
slab_file_in.close()

#write data
raw_data_1_file_out = open('ads_ene.dat', "w")

energy=[]
for i in energy_in:
    energy=i.split()
    for j in slab_in:
        slab=j.split()
        x = "%0.7f" %(float(float(energy[1]))+(float(slab[1])))
        print>>raw_data_1_file_out,slab[0], x

The output it generates is not what I want. I just want it to generate a list showing

A 5
B 7
C 9

Pls can someone suggest where I am going wrong with this code.

Thank you

Assuming both files are the same length (if they are not you will have to account for that), use the same offset for each list, i.e. record 1 from file a-->record 1 from file b

for ctr in len(energy_in):
    ## print them for testing
    print energy_in[ctr], slab_in[ctr]
    energy=energy_in[ctr].split()
    ##for j in slab_in:
    slab=slab_in[ctr].split()

    if energy[0] == slab[0]:
        x = "%0.7f" % (float(energy[1])+float(slab[1]))
    else:
        print "Recs not equal", energy, slab

If you want to find all "A"s and add them together, and the files do not correspond, then you should use a dictionary.

Pls can someone

Please don't turn this forum into a high school.

Edited 4 Years Ago by woooee: n/a

Assuming both files are the same length (if they are not you will have to account for that), use the same offset for each list, i.e. record 1 from file a-->record 1 from file b

for ctr in len(energy_in):
    ## print them for testing
    print energy_in[ctr], slab_in[ctr]
    energy=energy_in[ctr].split()
    ##for j in slab_in:
    slab=slab_in[ctr].split()

    if energy[0] == slab[0]:
        x = "%0.7f" % (float(energy[1])+float(slab[1]))
    else:
        print "Recs not equal", energy, slab

If you want to find all "A"s and add them together, and the files do not correspond, then you should use a dictionary. Please don't turn this forum into a high school.

You can't iterate over an integer ;)

@OP, your post doesn't really explain your code, nor provide good examples of input/output. If you want better help, you're going to need to try and write a better description of what you want to happen, what you have to make that happen and what you're not understanding.

The code the OP posted creates lists

energy_in=energy_file_in.readlines()
slab_in=slab_file_in.readlines()

Edited 4 Years Ago by woooee: n/a

Too late to change it now. I have no problems with the OP doing a little debugging anyway, and since this post was in many forums, probably someone else has already provided enough to answer the OP's question.

This article has been dead for over six months. Start a new discussion instead.