I need a little bit of help here with comparing data sets.

file1 = open("filename1.txt", "r")
file2 = open("filename2.txt", "r")

fileone = file1.readlines()
filetwo = file2.readlines()

file1 = close()
file2 = close()

exfile = open("results.txt", "w")

for line1 in fileone:
    s1, v1, p1 = line[:-1].split(":")

for line2 in filetwo:
    s2, v2, p2 = line[:-1].split(":")

        if st1 != st2 and vt1 != vt2:
            print("Please make sure you did not misspell column1 or change the number of votes and try again")

exfile.close()

So its a rough product here. I'm missing a line of code towards the end. Anyways the two files I'm opening will be divided into 3 columns with a colon. So essentially it'll look something like:

filename1.txt
abc:def:123
xyz:mel:345

filename2.txt
abc:def:125
xyz:mel:597

Pretty much the last column changes while the first two remains the same, generally speaking.

When I read 1 file, what I did was use a for loop to read each line similar to how I did it above. Now the issue is I have two files and I need to split each line of the file up similar to how I did it with 1 file.

The goal of the program is to read two files, output a new file with where column 1 and column 2 remains the same, but column 3 is replaced by the data from filename2.txt. Because I was hoping to use a while statement like:

while s1 == s2 and v1 == v2:
    exfile.write(s1,":",v1,":",p2)

Recommended Answers

All 5 Replies

You should save the split lines, not do readlines and overwrite variable with split data leaving the last lines split data in variables.

Indention and variables of line 18 does not make sense.

Whoops, I thought I removed it when I pasted the code.

I'm still a little new to split, read, etc since I've only been learning through bruteforcing methods.

Because each column is divided by a colon would the code be like this:

fileone.splitlines()

or

fileone.splitlines(":")

or would both net me the same results?

fileone.splitlines(":")

No that would make the only parameter keeplines 'True' value which means that the lines will have the newlines intact.

You must use normal split (or actually rsplit(':', 1) as you only want to use the line upto last colon as key) with list comprehension or for loop and append to list. Using dictionary with the key would be most efficient.

I'm reading up on dictionaries and I'm curious would it make sense if I did something like this:

f1_dict = {}
f1 = open("filename1.txt", "r")
for state in f1:
    s, v, p = line.strip.split(":")
    f1_dict[s] = p
f1.close()

And do this for the 2nd file, then do a 3rd one for the file I will write.

No, in that you are trying to split the function strip instead of calling it and you are loosing the v part info, is it really OK to discard?

Basically you are moving to right direction, though ;)

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.