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 />";
}
}
}
?>