Reputation:
I'm trying to read two files, and compare them on Python (2.7.3)
They don't have the same size/order, because I'm working with IDs/names and they won't "match".
And I don't want to read them simultaneously, but "file2" thorough and compare with each line of "file1" to then read another line of "file1" and so on
From what I've done, it works poorly, with some problems.
For example, look at this piece of the code:
if split_cronus[0] == split_data[0]:
The program executes everything in the 'if', and then exits.
If I call the function again, after:
print final_line + "\n"
It will work for 62 times and then show this error:
Traceback (most recent call last):
File "C:\Users\Matheus\Desktop\DBWolfmizator\DBWolfmizator\DBWolfmizator.py", line 40, in <module>
File "C:\Users\Matheus\Desktop\DBWolfmizator\DBWolfmizator\DBWolfmizator.py", line 28, in translate_itemdb
translate_itemdb()
The "line 28" error is shown everytime the program loops.
And then:
File "C:\Users\Matheus\Desktop\DBWolfmizator\DBWolfmizator\DBWolfmizator.py", line 15, in translate_itemdb
for line2 in data:
ValueError: I/O operation on closed file
Which means, with the 'if' there, I can get only one single match, like if the file had only one line; but with recursion, I can make it work a few more times before the second file ends.
If you didn't understand:
I have to read two files.
"file1" and "file2"
In theory, it's reading like this:
file1_line1 -> file2_line1
file1_line1 -> file2_line2
file1_line1 -> file2_line3
...
file1_line2 -> file2_line1
file1_line2 -> file2_line1
...
But when I got a match, the program exits from the loop.
How do I do that?
In PHP it works great, I was trying to make something like a "port".
Python code:
cronus = open("item_db.txt", "r+")
data = open("idnum2itemdisplaynametable.txt", 'r')
new_item = open("item_db_new.txt", 'w')
def translate_itemdb():
try:
try:
for line in cronus:
if line.startswith("//") or len(line) < 3:
new_item.write(line)
continue
split_cronus = str.split(line, ",")
del split_cronus[len(split_cronus) - 1]
for line2 in data:
if line2.startswith("//") or len(line2) < 3:
continue
split_data = str.split(line2, "#")
del split_data[len(split_data) - 1]
if split_cronus[0] == split_data[0]:
split_cronus[1] = str.replace(split_data[1], " ", "_")
split_cronus[2] = str.replace(split_data[1], "_", " ")
final_line = ','.join(split_cronus)
new_item.write(final_line + "\n")
print final_line + "\n"
finally:
cronus.close()
data.close()
new_item.close()
except IOError:
raise
return
translate_itemdb()
PHP code:
<?php
set_time_limit(0);
$Cronus = file('item_db.txt');
$Data = file('idnum2itemdisplaynametable.txt');
for( $i = 0; $Cronus[$i]; $i++ ) {
if( $Cronus[$i][0] == '/' || strlen($Cronus[$i]) < 3 ) {
echo $Cronus[$i]."<br />";
continue;
}
$ExplodeCronus = explode( ',', $Cronus[$i] );
for( $j = 0; @$Data[$j]; $j++ ) {
if( $Data[$j][0] == '/' || strlen($Data[$j]) < 3 )
continue;
$ExplodeData = explode( '#', $Data[$j] );
if( $ExplodeData[0] == $ExplodeCronus[0] ) {
$ExplodeCronus[1] = str_replace( " ", "_", $ExplodeData[1] );
$ExplodeCronus[2] = str_replace( "_", " ", $ExplodeData[1] );
$Linha = implode( ',', $ExplodeCronus );
echo $Linha."<br />";
}
}
}
?>
Upvotes: 0
Views: 1898
Reputation: 5821
Well, I would do something like this for comparing two files:
def compare_two_files(filename1, filename2):
input1 = open(filename1)
input2 = open(filename2)
lines1 = input1.readlines()
lines2 = input2.readlines()
# Iterate over the two files
for l1 in lines1:
cur_l1 = l1.split(',')
for l2 in lines2:
cur_l2 = l2.split(',')
# Compare file's lines
if cur_l1[0] == cur_l2[0]:
print('something')
I didn't test it but it should works.
Upvotes: 1
Reputation: 2323
One way to fix, is move this line:
data = open("idnum2itemdisplaynametable.txt", 'r')
To just before this line:
for line2 in data:
That way you re-open the data file as needed. (Adjust your exception handling as needed to close the data file. Consider using with
.)
Secondly, to get a closer port to the PHP code you posted, you could use readlines
. You are reading the file several times anyway, after all. Just read them all at once to start with and then do your processing.
So, leaving the data opening code where it is, you would do:
data_f = open("idnum2itemdisplaynametable.txt", 'r')
data = data_f.readlines()
Then your for line2 in data
loop is just revisiting an array of lines.
Upvotes: 2