I encountered a issue on using list in python.
with open('test.txt', 'r', encoding='utf-8') as f:
lines = f.readlines()
line = [l.rstrip('\n') for l in lines[:2]]
print(line[0])
ll = []
for lin in lines[2:]:
ll.append(lin)
break
print(line[0])
the result is:
0;;no2;;the third part3;;the fourth part4
0;;no2;;the third part3;;the fourth part4
But when I turn the variable lin to line.(the same name mentioned)
with open('test.txt', 'r', encoding='utf-8') as f:
lines = f.readlines()
line = [l.rstrip('\n') for l in lines[:2]]
print(line[0])
ll = []
for line in lines[2:]:
ll.append(line)
break
print(line[0])
the result is:
0;;no2;;the third part3;;the fourth part4
0
It apparently shows that the value of line has been change during the for loop when sharing the same variable name.