I'm attempting to create a map using Python and the beautiful soup library.
I am a newbie and am inexperienced with programming.
I am receiving the attached error and am unsure how to proceed. Presumably there is an issue with my data file (a csv file).
What is Python telling me? What does "new line character in unquoted field" mean?
Here is the code:
# Read in script rates
scriptrate = {}
min_value = 100; max_value = 0
reader = csv.reader(open('/users/gcameron/Desktop/map/data.csv'), delimiter=",")
for row in reader:
try:
county = row[1]
rate = float( row[2].strip() )
rate[county] = rate
except:
pass
enter image description here
-
This is your third question of the day?quantum– quantum2012年10月21日 19:14:49 +00:00Commented Oct 21, 2012 at 19:14
-
1Did you screw up your formatting or did you forget to indent all the code inside the for loop? Because right now that for is empty.Borgleader– Borgleader2012年10月21日 19:15:16 +00:00Commented Oct 21, 2012 at 19:15
-
maybee its an option you are missing in csv.readertobspr– tobspr2012年10月21日 19:15:53 +00:00Commented Oct 21, 2012 at 19:15
2 Answers 2
The error message says that it's something with your file, and the problem is on this line.
reader = csv.reader(open('/users/gcameron/Desktop/map/data.csv'), delimiter=",")
It says you need to change to:
reader = csv.reader(open('/users/gcameron/Desktop/map/data.csv', 'rU'), delimiter=",")
Notice the 'rU', it tells python to read the file in universal newline mode, as said in the error message. I am not sure this will help, but you should try it.
You should also attach your csv file, as I believe the problem lies in the file if the above method doesn't work.
1 Comment
This problem occurs because of the different new line formats in different operating systems. Python, by default, assumes new-lines to be Unix style LF (\n) and if this is not the case then you have a problem.
In your case, your .csv may have very well come from Windows and hence all excel quirks(formatting issues). You can fix it by opening in universal new-line mode.
Change: open(r'/users/gcameron/Desktop/map/data.csv', 'rU')