gpt4 book ai didi

python - 获取 "new-line character seen in unquoted field"读取 csv 文件时出错

转载 作者:行者123 更新时间:2023-11-28 19:06:55 28 4
gpt4 key购买 nike

我正在尝试将 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,,,,,,

最佳答案

嗯,你的代码中有一些错误:

首先,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'}
[...]

关于python - 获取 "new-line character seen in unquoted field"读取 csv 文件时出错,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45738905/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com