获取“在未引用的字段中看到的新行字符”错误读取csv文件

时间:2017-08-17 15:13:28

标签: python

我正在尝试将csv文件读入程序,我收到以下四个错误。

File "/Users/interpott/Downloads/SatStressGUI-master/Contents/Resources/satstressgui.py", line 2173, in load
File "/Users/interpott/Downloads/SatStressGUI-master/Contents/Resources/satstressgui.py", line 891, in file_dialog
File "/Users/interpott/Downloads/SatStressGUI-master/Contents/Resources/satstressgui.py", line 887, in file_dir_dialog
File "/Users/interpott/Downloads/SatStressGUI-master/Contents/Resources/satstressgui.py", line 2203, in load_entries
Error: new-line character seen in unquoted field - do you need to open the file in universal-newline mode?

代码段:

def load(self, evt):
    try:
        file_dialog(self,
                    message=u"Load from CSV file",
                    style=wx.OPEN,
                    wildcard='CSV files (*.csv)|*.csv',
                    action=self.load_entries) **2173**
    except Exception, e:
        traceback.print_exc()

def set_num_rows(self,num_rows):
    self.pp.SetRows(num_rows)
    self.sp.SetRows(num_rows)
    self.tp.SetRows(num_rows)
    if (num_rows > self.rows):
        for j in range(num_rows-self.rows):
            self.add_row(self.fieldPanel,self.pp, self.header1, '0')
            self.add_row(self.fieldPanel,self.tp, self.header2, '')
            self.add_row(self.fieldPanel,self.sp, self.header3, '')
        self.update_parameters()
    else:
        for j in range(self.rows-num_rows):
            for p,d in self.header1+self.header2+self.header3:
                self.parameters[p][-1].Destroy()
                del self.parameters[p][-1]
                del self.sc.parameters[p][-1]
    self.rows = num_rows
    self.row_ctrl.SetValue(num_rows)
    self.spin_value = num_rows
    self.sc.set_parameter('point_rows',self.rows)
    self.fieldPanel.Layout()
    self.fieldPanel.SetupScrolling()

def load_entries(self, filename):
    f = open(filename)
    csvreader = csv.reader(f, dialect=csv.excel_tab)
    coord = csvreader.next() #Skip headers. **2203**
    data = list(csvreader)
    self.set_num_rows(len(data))
    try:
        keys = ['theta', 'phi', 't', 'orbit']

        for i,coord in enumerate(data):

            for key in keys:
                val = coord[keys.index(key)]
                self.parameters[key][i+1].SetValue(val)
                self.sc.set_parameter(key, val, point = i+1)
    except:
        traceback.print_exc()
    finally:
        f.close()
        self.fieldPanel.Layout()
        self.fieldPanel.SetupScrolling()
        self.Layout()

我尝试了一些常见的建议。谁能告诉我在哪里弄乱?

以下示例CSV文件:

theta [degrees],phi [degrees],t [yrs],orbital pos [degrees],Stt [kPa],Spt [kPa],Spp [kPa],sigma1 [kPa],sigma3 [kPa],alpha [degrees]
10,10,0,0,,,,,,
10,10,1000,0,,,,,,
10,10,2000,0,,,,,,
10,10,3000,0,,,,,,
10,10,4000,0,,,,,,
10,10,5000,0,,,,,,
10,10,6000,0,,,,,,
10,10,7000,0,,,,,,
10,10,8000,0,,,,,,
10,10,9000,0,,,,,,
10,10,10000,0,,,,,,
10,10,11000,0,,,,,,
10,10,12000,0,,,,,,
10,10,13000,0,,,,,,
10,10,14000,0,,,,,,
10,10,15000,0,,,,,,
10,10,16000,0,,,,,,
10,10,17000,0,,,,,,
10,10,18000,0,,,,,,
10,10,19000,0,,,,,,
10,10,20000,0,,,,,,
10,10,21000,0,,,,,,
10,10,22000,0,,,,,,
10,10,23000,0,,,,,,
10,10,24000,0,,,,,,
10,10,25000,0,,,,,,

1 个答案:

答案 0 :(得分:0)

嗯,您的代码中存在一些错误:

首先,CSV分隔符不是标签,而是逗号,因此请使用Excel方言替换:

dialect=csv.excel

在Python2中,您需要以二进制模式打开CSV文件。

您的文件读取可以按照以下步骤完成:

import csv

keys = ['theta', 'phi', 't', 'orbit']

with open(filename, mode="rb") as fd:
    csv_reader = csv.reader(fd, dialect=csv.excel)
    header = next(csv_reader)
    for row in csv_reader:
        values = dict(zip(keys, row[:len(keys)]))
        print(values)

打印:

{'theta': '10', 'phi': '10', 't': '0', 'orbit': '0'}
{'theta': '10', 'phi': '10', 't': '1000', 'orbit': '0'}
{'theta': '10', 'phi': '10', 't': '2000', 'orbit': '0'}
{'theta': '10', 'phi': '10', 't': '3000', 'orbit': '0'}
{'theta': '10', 'phi': '10', 't': '4000', 'orbit': '0'}
{'theta': '10', 'phi': '10', 't': '5000', 'orbit': '0'}
[...]