【问题标题】:Incorrect date value when loading xlsx file to table using pymysql and xlrd使用 pymysql 和 xlrd 将 xlsx 文件加载到表时的日期值不正确
【发布时间】:2020-06-04 02:41:37
【问题描述】:

(非常)python 初学者用户在这里。我正在尝试使用 xlrd 和 pymysql python 库将 xlsx 文件加载到 MySQL 表中,但出现错误:

pymysql.err.InternalError: (1292, "Incorrect date value: '43500' for column 'invoice_date' at row 1")

我的表的invoice_date 的数据类型是DATE。我的 xlsx 文件中此字段的格式也是日期。如果我将表数据类型更改为 varchar,一切正常,但我更愿意将数据作为日期加载到我的表中,而不是事后转换。关于我为什么会收到此错误的任何想法?看来 xlrd 或 pymysql 正在我的 xlxs 文件中读取 '2/4/2019' 作为 '43500' 并且 mysql 由于数据类型不匹配而拒绝它。

import xlrd
import pymysql as MySQLdb

# Open workbook and define first sheet
book = xlrd.open_workbook("2019_Complete.xlsx")
sheet = book.sheet_by_index(0)

# MySQL connection
database = MySQLdb.connect (host="localhost", user="root",passwd="password", db="vendor")

# Get cursor, which is used to traverse the databse, line by line
cursor = database.cursor()

# INSERT INTO SQL query
query = """insert into table values (%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s)"""

# Create a For loop to iterate through each row in the XLS file, starting at row 2 to skip the headers
for r in range(1, sheet.nrows):
    lp = sheet.cell(r,0).value
    pallet_lp = sheet.cell(r,1).value
    bol = sheet.cell(r,2).value
    invoice_date = sheet.cell(r,3).value
    date_received = sheet.cell(r,4).value
    date_repaired = sheet.cell(r,5).value
    time_in_repair = sheet.cell(r,6).value
    date_shipped = sheet.cell(r,7).value
    serial_number = sheet.cell(r,8).value
    upc = sheet.cell(r,9).value
    product_type = sheet.cell(r,10).value
    product_description = sheet.cell(r,11).value
    repair_code = sheet.cell(r,12).value
    condition = sheet.cell(r,13).value
    repair_cost = sheet.cell(r,14).value
    parts_cost = sheet.cell(r,15).value
    total_cost = sheet.cell(r,16).value
    repair_notes = sheet.cell(r,17).value
    repair_cap = sheet.cell(r,18).value
    complaint = sheet.cell(r,19).value
    delta = sheet.cell(r,20).value

    # Assign values from each row
    values = (lp, pallet_lp, bol, invoice_date, date_received, date_repaired, time_in_repair, date_shipped, serial_number, upc, product_type, product_description, repair_code, condition, repair_cost, parts_cost, total_cost, repair_notes, repair_cap, complaint, delta)

    # Execute sql Query
    cursor.execute(query, values)

# Close the cursor
cursor.close()

# Commit the transaction
database.commit()

# Close the database connection
database.close()

# Print results
print ("")
columns = str(sheet.ncols)
rows = str(sheet.nrows)
print ("I just imported " + columns + " columns and " + rows + " rows to MySQL!")

【问题讨论】:

    标签: python mysql date xlrd pymysql


    【解决方案1】:

    您可以查看this answer 以获得更详细的说明,但基本上 Excel 将日期视为相对于 1899-12-31 的数字,因此要将您的日期值转换为实际日期,您需要将该数字转换为MySQL 将接受的 ISO 格式日期。您可以使用date.fromordinaldate.isoformat 来做到这一点。例如:

    dval = 43500
    d = date.fromordinal(dval + 693594)
    print(d.isoformat())
    

    输出:

    2019-02-04
    

    【讨论】:

    • 谢谢!我最终需要做一些简单的格式化,但我最终对每个日期字段都做了这样的事情。 idf = date.fromordinal(int(invoice_date + 693594)) invoice_date_fixed = idf.isoformat()
    猜你喜欢
    • 1970-01-01
    • 2021-12-01
    • 2017-11-04
    • 2021-07-16
    • 2019-08-06
    • 2015-05-29
    • 2021-03-27
    • 1970-01-01
    • 2011-12-30
    相关资源
    最近更新 更多