简体   繁体   中英

An error occured when reading lines by readlines() method

The following function is defined to read the content of the .csv file and store it into lists:

def _read_txt_file(self):
    self.img_paths = []
    self.labels = []
    with open(self.txt_file, 'r') as f:
        lines = f.readlines()
        for line in lines:
            items = line.split(',')  # .csv
            self.img_paths.append(items[0])
            self.labels.append(int(items[1]))

Something wrong has happened in line 6. The PyCharm debugger shows normal values of variable lines , ie '../ZP_45151/0.bin,0\\n' , '../ZP_45151/0.bin,1\\n' and so forth. But the line variable displays a weird value, that is, ',\\n' , which is obviously that something has been lost. Does anyone have an idea that why the operation is not going through the complete content?

Well, I checked my reading .csv file and found that there are plenty of blank lines expressed as ',\\n' in string format. The issue has been settled.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM