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

4

2 回答 2

2

错误消息说它与您的文件有关,问题出在这一行。

reader = csv.reader(open('/users/gcameron/Desktop/map/data.csv'), delimiter=",")

它说您需要更改为:

reader = csv.reader(open('/users/gcameron/Desktop/map/data.csv', 'rU'), delimiter=",")

注意'rU',它告诉 python 以通用换行模式读取文件,如错误消息中所述。我不确定这会有所帮助,但您应该尝试一下。

您还应该附上您的 csv 文件,因为如果上述方法不起作用,我认为问题出在文件中。

于 2012-10-21T19:16:41.163 回答
0

出现此问题的原因是不同操作系统中的新行格式不同。默认情况下,Python 假定换行符是 Unix 风格的 LF ( \n),如果不是这种情况,那么你就有问题了。

在您的情况下,您的 .csv 很可能来自 Windows,因此所有 excel 怪癖(格式问题)。您可以通过在通用换行模式下打开来修复它。改变:open(r'/users/gcameron/Desktop/map/data.csv', 'rU')

于 2012-10-21T19:21:03.970 回答