0

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

3
  • This is your third question of the day?
    – quantum
    Oct 21, 2012 at 19:14
  • 1
    Did 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
    Oct 21, 2012 at 19:15
  • maybee its an option you are missing in csv.reader
    – tobspr
    Oct 21, 2012 at 19:15

2 Answers 2

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
  • Thanks for all your help today xiaomao
    – Doug Fir
    Oct 21, 2012 at 19:18
0

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')

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.