2016-11-09 65 views
0

我試圖從Excel電子表格導入數據到使用Python的訪問數據庫。我已經創建了腳本,但我在日期字段中遇到了一些問題。目前在Excel電子表格2周的cols包含日期(Problem_due_date和Root_cause_identified)Python - Excel導入無法訂購的類型空單元格

它的數據插入到表不錯,但它插入數據作爲文本

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與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電子表格中的空白單元格打開後我就意識到它失敗了。

我該如何跳過這些單元格或重新格式化它們?

回答

1

由於您試圖將文本('',即空白)值轉換爲日期,並且僅適用於數值,因此發生錯誤。如果要只轉換日期值(格式爲日期浮點數),你可以使用這樣的代碼:

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... 
    # e.g. 
    # values[18] = values[18] or datetime.datetime.now() 
    cur.execute(query, values) 

您可能還需要修復了values[7]Problem_due_date)和values[18]Root_cause_identified)如果他們不是datetime值。

相關問題