When the file reads the content, it is possible to find open a file, and then read to the list or dict can only read once, and then define a variable to be assigned to the contents of the file will be removed null value.
With open ('./goods.txt ', ' R ') as FG:
result = List (Line.strip (). Split (': ') for line on FG if line)
RESULT2 = Dict (Line.strip (). Split (': ') for line on FG if line) #取出为空值
Cause: This is because when the file is read, the pointer moves from the beginning of the file to the end of the file, and then when the variable is defined, it starts reading from the end of the file.
Solution: Combine seek, tell method
With open ('./goods.txt ', ' R ') as FG:
result = List (Line.strip (). Split (': ') for line on FG if line)
Fg.seek (0)
RESULT2 = Dict (Line.strip (). Split (': ') for line on FG if line)
Tell method-the position where the character is removed
Python 3.x--file usage of seek, tell