CSV New-Line Character Seen in Unquoted Field Error

CSV new-line character seen in unquoted field error

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

CSV read error: new-line character seen in unquoted field

From PEP-0278:

In a Python with universal newline support open() the mode parameter
can also be "U", meaning "open for input as a text file with universal
newline interpretation". Mode "rU" is also allowed, for symmetry with
"rb"

So try to change

with open('./Destinations.csv', 'r') as csvfile:

to

with open('./Destinations.csv', 'rb') as csvfile:

If the error persists, change to

with open('./Destinations.csv', 'rU') as csvfile:

Edited accorded to Martijn Pieters's comment.

new-line character seen in unquoted field'-error when trying to write to a csv-file in Python

i suggest using re.sub (python) to manipulating regex

import fileinput
import re

logfile="error.log"
outfile="clean.csv"

with open("error.log") as f:
newText = f.read()
newText = re.sub(r" ", "", newText)
newText = re.sub(r"(", "", newText)
newText = re.sub(r")", "\n", newText)

with open("clean.csv", "w") as f:
f.write(newText)

if you want to add some parameter in first line .csv file, just add it at the end of source code above:

for line in fileinput.input(files=['clean.csv'], inplace=True):
if fileinput.isfirstline():
print 'parameter1,parameter2,parameter3, .... '
print line,

hope this answer help you ^_^ cheers

new-line character seen in unquoted field

I found the solution in another post

The issue was how i was saving the .csv file. When producing a .csv file in excel for mac save it as "Windows Comma Separated Values (.csv)" This will stop the addition of unwanted characters that throw of import csv in Django and python.



Related Topics



Leave a reply



Submit