首页
学习
活动
专区
工具
TVP
发布
社区首页 >问答首页 >如何跳过列的“?(将EXCEL文件python导入MySQL)

如何跳过列的“?(将EXCEL文件python导入MySQL)
EN

Stack Overflow用户
提问于 2018-07-30 00:03:51
回答 1查看 0关注 0票数 0

我想插入日期时间值mysql。所以我制作了mysql时间戳并修复了excel日期YYYYMMDDHHmmss类型。当我尝试导入excel文件python到mysql成功获取excel col中的完整数据。但是包含空列有内部错误1292,“第1行的列'AriT'的日期时间值:''不正确”。我想要空列跳过或在mysql中保存null。怎么编码?

代码语言:javascript
复制
import xlrd
import pymysql

#open the workbook and define rhe worksheet
book  = xlrd.open_workbook("d:/final/final.xls")
sheet = book.sheet_by_index(0)
#sheet = book.sheet_by_index(0)

#establish a mysql connection
database = pymysql.connect(host="localhost", user='root', password='1234',db='test')

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


#create the insert into sql quety
query = """INSERT INTO test2 (AriT) VALUES (%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):
    AriT = sheet.cell(r,9).value    

#assign calues from each row
 values = (AriT )

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

#close the cursor
cursor.close()

#commit the transaction
database.commit()

#close the databases connection
database.close()
EN

回答 1

Stack Overflow用户

发布于 2018-07-30 09:39:58

如果要将值检索为在Excel中指定的格式,则应使用Text

例如,公式ActiveCell=Now() ,我们已经设置了格式YYYYMMDDHHmmss

让我们在立即窗口中尝试(在VBA编辑器中按Ctrl + G)

代码语言:javascript
复制
? ActiveCell.Value
30/7/2018 09:20:28 
? ActiveCell.Value2
 43311.3892158565 
? ActiveCell.Text
20180730092028

但是,如果你的单元格值超过单元格宽度,.Text则将变为###

在你的情况下,你可以使用Format(ActiveCell.Value2, "YYYYMMDDHHmmss")检索值。 这样你就不必担心单元格的格式化了。

代码语言:javascript
复制
? Format(ActiveCell.Value2, "YYYYMMDDHHmmss")
20180730092738
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/-100001706

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档