Read with open as: the problem when reading the file

import codecs
import  os


data_dir="./2020"
label_file="label_list.txt"
label_list = os.path.join(data_dir, label_file)
with open(label_list, encoding='utf-8') as flist:
    print(flist.read())
    lines = [line.strip() for line in flist]
    #print(lines)

    for line in lines:
        parts = line.strip().split()

Insert picture description here

import codecs
import  os


data_dir="./2020"
label_file="label_list.txt"
label_list = os.path.join(data_dir, label_file)
with open(label_list, encoding='utf-8') as flist:
    #print(flist.read())
    lines = [line.strip() for line in flist]
    print(lines)

    for line in lines:
        parts = line.strip().split()

Insert picture description here
The first opened file here is saved in flist, lines is the parse list. From the second picture, we can see that line reads one line at a time to form a list, and then strip() removes the spaces at the beginning and the end to generate a list of lines.

import codecs
import  os


data_dir="./2020"
label_file="label_list.txt"
label_list = os.path.join(data_dir, label_file)
with open(label_list, encoding='utf-8') as flist:
    print(flist.read())
    lines = [line.strip() for line in flist]
    print(lines)

    for line in lines:
        parts = line.strip().split()

Insert picture description here
This phenomenon is not known yet, but it is probably related to the flow, so please record it first.

Guess you like

Origin blog.csdn.net/ALZFterry/article/details/109312862