Jan-27-2023, 06:32 AM
Greetings!
I’m writing Python snippets here and there but I could never understand how Python iterates over a file.
It is kind of a mystery, a black box or something…
Here is an example. I’d like to merge two files.
File1:
1
2
3
File2:
1
2
3
4
5
6
7
And the snippet to merge files.
But I got :
-- 1
++ 2
++ 3
Could anyone help me with this?
I even bought a Python book, it has like 3000000 pages but I still do not get it.
Why I cannot get the right printout?
Thank you.
I’m writing Python snippets here and there but I could never understand how Python iterates over a file.
It is kind of a mystery, a black box or something…
Here is an example. I’d like to merge two files.
File1:
1
2
3
File2:
1
2
3
4
5
6
7
And the snippet to merge files.
with open("C:/01/file1.txt", 'r') as f: with open("C:/01/file2.txt", 'r') as fe: for line_f in f: for line_fe in fe: if line_f == line_fe: print(f"-- {line_f}") else: print(f"++ {line_f}")I was expecting out up as 1,2,3,4,5,6,7
But I got :
-- 1
++ 2
++ 3
Could anyone help me with this?
I even bought a Python book, it has like 3000000 pages but I still do not get it.
Why I cannot get the right printout?
Thank you.