我试图访问Microsoft Access数据库表中的一些列,其中包含double类型的数字,但我收到标题中提到的错误。用于查询数据库的代码如下所示,错误发生在执行cur.execute(....)命令的行中。基本上我正在尝试过滤掉在特定时间间隔内捕获的数据。如果我在cur.execute(....)命令中排除包含双数据类型的列CM3_Up,CG3_Up,CM3_Down,CG3_Down,我将不会收到错误。使用相同的逻辑从其他表访问双数据类型,它工作正常,我不知道出了什么问题。
代码:
start =datetime.datetime(2015,03,28,00,00)
a=start
b=start+datetime.timedelta(0,240)
r=7
while a < (start+datetime.timedelta(1)):
params = (a,b)
sql = "SELECT Date_Time, CM3_Up, CG3_Up, CM3_Down, CG3_Down FROM
Lysimeter_Facility_Data_5 WHERE Date_Time >= ? AND Date_Time <= ?"
for row in cur.execute(sql,params):
if row is None:
continue
r = r+1
ws.cell(row = r,column=12).value = row.get('CM3_Up')
ws.cell(row = r,column=13).value = row.get('CG3_Up')
ws.cell(row = r,column=14).value = row.get('CM3_Down')
ws.cell(row = r,column=15).value = row.get('CG3_Down')
a = a+five_min
b = b+five_min
wb.save('..\SE_SW_Lysimeters_Weather_Mass_Experiment-02_03_26_2015.xlsx')
完整的错误报告:
Traceback (most recent call last):
File "C:\DB_PY\access_mdb\db_to_xl.py", line 318, in <module>
for row in cur.execute(sql,params):
File "build\bdist.win32\egg\pypyodbc.py", line 1920, in next
row = self.fetchone()
File "build\bdist.win32\egg\pypyodbc.py", line 1871, in fetchone
value_list.append(buf_cvt_func(alloc_buffer.value))
ValueError: could not convert string to float: E-3
答案 0 :(得分:0)
听起来你收到了像#2.; 2.34E-3&#39;这样的字符串,所以尝试转换。不了解Python,但在C#中它可能就像:
ws.cell(row = r,column=12).value = Convert.ToDouble(row.get('CM3_Up'))
ws.cell(row = r,column=13).value = Convert.ToDouble(row.get('CG3_Up'))
ws.cell(row = r,column=14).value = Convert.ToDouble(row.get('CM3_Down'))
ws.cell(row = r,column=15).value = Convert.ToDouble(row.get('CG3_Down'))
答案 1 :(得分:0)
关于这个讨论: Python: trouble reading number format
问题可能是e应该是d,例如:float(row.get('CM3_Up').replace('E', 'D'))
虽然听起来很奇怪,但我对Python知之甚少。