5
Personal I am with a doubt and a great difficulty to create a program that reads 2 files, example doc1.txt
and doc2.txt
, within the files has the following data:
doc1.txt doc2.txt
1 1
1 2
2 5
3 6
4 7
5 7
10 8
9
It should be line by line and compare, for example if doc1 data 1 is == to doc2 data 1, doc1 > doc2 or <, record the data in a doc called result.txt in sequence with each given line by line. He must record the data in sequence.
Just follow my code:
#Arquivos de Texto
with open('doc1.txt', 'r', encoding='UTF-8') as doc1:
arq1: int= doc1.readline()
with open('doc2.txt', 'r', encoding='UTF-8') as doc2:
arq2: int = doc2.readline()
if arq1 == arq2:
with open('resultado.txt', 'w') as res:
r: int = res.write(arq1)
elif arq1 > arq2:
with open('resultado.txt', 'w') as res:
r: int = res.write(arq1)
elif arq1 < arq2:
with open('resultado.txt', 'w') as res:
r: int = res.write(arq2)
else:
print('Error')
It only compares the first data 1 == 1 and writes to the file resultado.txt
and then he doesn’t read any more data from the files.
I don’t know what I’m doing wrong.
You used the method
readline
which, as the name says, reads a line. If you want to compare all the lines, you need to iterate on them. It is not clear what needs to be saved in the result file. It will always be the highest among the values or the file name?– Woss
Then I must use readlines, to read all lines, the data that will be written in the result file will be compared, example both doc1 and doc2 files have 1, then the files will be compared, if 1 is equal to 1, will be written in the result file, ai it will compare the next line 1 is equal to 2, no, 1 is greater than 2, no, 1 is less than 2 yes then write to the result, it always reads the first file and is saved in the variable to then compare with the next one, until all the result is in sequence ex: 1,2,3,4,5,6,7,8,9,10. is more or less that.
– Gui Scholz Portela
@Guischolzportela: What if the files don’t have the same number of lines ? What is the expected output for the example you gave ?
– Lacobus
Worse than I don’t know, I used to do a program in Cobol called balance line, in other words, he was comparing numbers online per line, as I explained above, then he was recording in sequence in numerical order per line, regardless of the amount of data inside the files, some files had 1000 lines and another 500 lines, and still worked.
– Gui Scholz Portela