0

我正在尝试访问 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
4

2 回答 2

0

关于这个讨论: Python:阅读数字格式时遇到问题

问题可能是 e 应该是 d,例如:

float(row.get('CM3_Up').replace('E', 'D'))

虽然对我来说听起来很奇怪,但我对 Python 知之甚少。

于 2015-05-01T07:06:32.857 回答
0

听起来您收到了像“2.34E-3”这样的字符串,所以请尝试转换。不知道 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'))
于 2015-04-15T20:31:57.673 回答