Python – Excel导入无法编辑的types空单元格

我试图从Excel电子表格导入数据到使用Python的访问数据库。 我已经创build了脚本,但我有一些date字段的问题。 目前Excel电子表格中有两列包含date(Problem_due_date和Root_cause_identified)

它将数据插入到表格中,但将数据作为文本插入

import pypyodbc import xlrd import datetime book = xlrd.open_workbook("I:\Documents\Management\ReportAutomationProject\EMEA YTD.xls") sheet = book.sheet_by_name("Page 1") conn = pypyodbc.connect( r"Driver={Microsoft Access Driver (*.mdb, *.accdb)};" + r"Dbq=I:\Documents\Management\ReportAutomationProject\Data.accdb;") cur = conn.cursor() query = """insert into Problems ([Number], Title, Active, Causing_IT_Service, Causing_Application, Causing_application_Instance, Incident_Severity, Problem_due_date, Assignment_group, Assignee, Impacted_countries, Impacted_regions, Business_impact_description, Workaround, Root_cause, Root_cause_level_3, Root_cause_level_2, Root_cause_level_1, Root_cause_identified, Causing_organisation, Problem_Summary, Activity_due, Approval_status, Major_incident, Approval_history, Approval_set, Business_duration, Duration) VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)""" for r in range(1, sheet.nrows): Number = sheet.cell(r, 0).value Title = sheet.cell(r, 1).value Active = sheet.cell(r, 2).value Causing_IT_Service = sheet.cell(r, 3).value Causing_Application = sheet.cell(r, 4).value Causing_application_Instance = sheet.cell(r, 5).value Incident_Severity = sheet.cell(r, 6).value Problem_due_date = sheet.cell(r, 7).value Problem_due_date = datetime.datetime(*xlrd.xldate_as_tuple(Problem_due_date, book.datemode)) Assignment_group = sheet.cell(r, 8).value Assignee = sheet.cell(r, 9).value Impacted_countries = sheet.cell(r, 10).value Impacted_regions = sheet.cell(r, 11).value Business_impact_description = sheet.cell(r, 12).value Workaround = sheet.cell(r, 13).value Root_cause = sheet.cell(r, 14).value Root_cause_level_3 = sheet.cell(r, 15).value Root_cause_level_2 = sheet.cell(r, 16).value Root_cause_level_1 = sheet.cell(r, 17).value Root_cause_identified=sheet.cell(r, 18).value Root_cause_identified=datetime.datetime(*xlrd.xldate_as_tuple(Root_cause_identified, book.datemode)) Causing_organisation = sheet.cell(r, 19).value Problem_Summary = sheet.cell(r, 20).value Activity_due = sheet.cell(r, 21).value Approval_status = sheet.cell(r, 22).value Major_incident = sheet.cell(r, 23).value Approval_history = sheet.cell(r, 24).value Approval_set = sheet.cell(r, 25).value Business_duration = sheet.cell(r, 26).value Duration = sheet.cell(r, 27).value values = (Number, Title, Active, Causing_IT_Service, Causing_Application, Causing_application_Instance, Incident_Severity, Problem_due_date, Assignment_group, Assignee, Impacted_countries, Impacted_regions, Business_impact_description, Workaround, Root_cause, Root_cause_level_3, Root_cause_level_2, Root_cause_level_1, now, Causing_organisation, Problem_Summary, Activity_due, Approval_status, Major_incident, Approval_history, Approval_set, Business_duration, Duration) cur.execute(query, values) sql = """ select * from Problems """ cur.execute(sql) for results in cur: print(results) cur.close() conn.commit() conn.close() 

我已经尝试运行使用datetime.datetime与xlrd.xldate_as_tuple这似乎工作,但即时通讯收到此错误:

 C:\Python34\python.exe C:/Users/d/PycharmProjects/untitled/test.py Traceback (most recent call last): File "C:/Users/d/PycharmProjects/untitled/test.py", line 11, in <module> a1_as_datetime = datetime.datetime(*xlrd.xldate_as_tuple(a1, book.datemode)) File "C:\Python34\lib\site-packages\xlrd\xldate.py", line 65, in xldate_as_tuple if xldate < 0.00: TypeError: unorderable types: str() < float() 

然后,我钻了进来,把它带回来。 当我把它从for循环中,它工作正常。 然后,我已经添加到一个普通的打印声明,我已经意识到它的失败,只要它击中Excel电子表格中的空白单元格。

我将如何跳过这些单元格或重新格式化它们?

错误发生是因为您正在尝试将文本('',即空白)值转换为date,并且只能用于数字值。 要只转换date值(浮动格式为date),你可以使用这样的代码:

 for r in range(1, sheet.nrows): values = [ datetime.datetime(*(xlrd.xldate_as_tuple(v, book.datemode))) if t == xlrd.XL_CELL_DATE else v for t, v in zip(sheet.row_types(r), sheet.row_values(r)) ] # if '' isn't acceptable for the date columns, you may need to fix them here... # eg # values[18] = values[18] or datetime.datetime.now() cur.execute(query, values) 

如果它们不是date时间值,您可能仍然需要修正values[7]Problem_due_date )和values[18]Root_cause_identified )。