CSV new-line character seen in unquoted field error

后端 未结 9 1220
花落未央
花落未央 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:26

    I realize this is an old post, but I ran into the same problem and don't see the correct answer so I will give it a try

    Python Error:

    _csv.Error: new-line character seen in unquoted field
    

    Caused by trying to read Macintosh (pre OS X formatted) CSV files. These are text files that use CR for end of line. If using MS Office make sure you select either plain CSV format or CSV (MS-DOS). Do not use CSV (Macintosh) as save-as type.

    My preferred EOL version would be LF (Unix/Linux/Apple), but I don't think MS Office provides the option to save in this format.

    0 讨论(0)
  • 2020-11-27 14:28

    If this happens to you on mac (as it did to me):

    1. Save the file as CSV (MS-DOS Comma-Separated)
    2. Run the following script

      with open(csv_filename, 'rU') as csvfile:
          csvreader = csv.reader(csvfile)
          for row in csvreader:
              print ', '.join(row)
      
    0 讨论(0)
  • 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
    
    0 讨论(0)
  • 2020-11-27 14:37

    Try to run dos2unix on your windows imported files first

    0 讨论(0)
  • 2020-11-27 14:37

    This worked for me on OSX.

    # allow variable to opened as files
    from io import StringIO
    
    # library to map other strange (accented) characters back into UTF-8
    from unidecode import unidecode
    
    # cleanse input file with Windows formating to plain UTF-8 string
    with open(filename, 'rb') as fID:
        uncleansedBytes = fID.read()
        # decode the file using the correct encoding scheme
        # (probably this old windows one) 
        uncleansedText = uncleansedBytes.decode('Windows-1252')
    
        # replace carriage-returns with new-lines
        cleansedText = uncleansedText.replace('\r', '\n')
    
        # map any other non UTF-8 characters into UTF-8
        asciiText = unidecode(cleansedText)
    
    # read each line of the csv file and store as an array of dicts, 
    # use first line as field names for each dict. 
    reader = csv.DictReader(StringIO(cleansedText))
    for line_entry in reader:
        # do something with your read data 
    
    0 讨论(0)
  • 2020-11-27 14:37

    Alternative and fast solution : I faced the same error. I reopened the "wierd" csv file in GNUMERIC on my lubuntu machine and exported the file as csv file. This corrected the issue.

    0 讨论(0)
提交回复
热议问题