Excel에서 스프레드 시트의 데이터를 Python을 사용하여 액세스 데이터베이스로 가져 오려고합니다. 스크립트를 만들었지 만 날짜 필드에 문제가 있습니다. 이 날짜 (Problem_due_date 및 Root_cause_identified)Python - Excel에서 비어있는 형식의 빈 셀 가져 오기
그것은 테이블 벌금에 데이터를 삽입이 포함 된 Excel 스프레드 시트에서 두 COLS은 현재 그러나 나는 날짜를 사용하여 실행 시도
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 스프레드 시트에서 빈 셀에 도달하자마자 실패한 것을 깨달았다.
어떻게하면이 셀을 건너 뛰거나 다시 포맷 할 수 있습니까?