CSV new-line character seen in unquoted field error

后端 未结 9 1265
花落未央
花落未央 2020-11-27 14:14

the following code worked until today when I imported from a Windows machine and got this error:

new-line character seen in unquoted field - do you need to o

9条回答
  •  离开以前
    2020-11-27 14:31

    It'll be good to see the csv file itself, but this might work for you, give it a try, replace:

    file_read = csv.reader(self.file)
    

    with:

    file_read = csv.reader(self.file, dialect=csv.excel_tab)
    

    Or, open a file with universal newline mode and pass it to csv.reader, like:

    reader = csv.reader(open(self.file, 'rU'), dialect=csv.excel_tab)
    

    Or, use splitlines(), like this:

    def read_file(self):
        with open(self.file, 'r') as f:
            data = [row for row in csv.reader(f.read().splitlines())]
        return data
    

提交回复
热议问题