【发布时间】:2018-07-17 10:45:39
【问题描述】:
我制作了一个程序,因为它适用于简单的 Excel 工作表,所以它的行为表现很奇怪。
我可以简单描述一下我的问题:
也是 Hour disappers 的头部,我尝试了 header = None 和 parscols = ["", ""],但头部仍然不会加入数据库
excel表格的数值不会入库,虽然我已经避免了缺失数据。
这是我的代码:
from src.server.connectToDB import get_sql_conn
import pandas as pd
if __name__ == '__main__':
cursor = get_sql_conn().cursor()
local_files = 'C:\\Users\\dersimw\\Source\Repos\\nordpoolAnalyse\\data\\2011-3.xlsx'
excelFile = pd.ExcelFile(local_files)
ark = ["01", "02", "03", "04", "05", "06", "07", "08", "09", "10", "11", "12", "13", "14", "15",
"16", "17", "18", "19", "20", "21", "22", "23", "24", "25", "26", "27", "28", "29", "30", "31"]
for sheets in ark:
df = excelFile.parse(sheets).head(5)
print(df.dropna(axis=1, how='all'))
for key, rows in df.items():
print("# Kolonne: ", "\n")
columnInsertSql = "INSERT INTO DataSets (Hour, BlockBuyNet, BlockSell, RejectedBlockBuy, RejectedBlockSell, NetImports) VALUES"
rowCounter = 0
for column in rows.items():
columnInsertSql += str(column)
if rowCounter != len(list(rows.items())):
columnInsertSql += ", "
rowCounter += 1
print("## SQL: " + columnInsertSql)
cursor.execute(columnInsertSql)
cursor.commit()
这是我打印df.dropna (axis = 1, how = 'all')时的结果:
小时 0 1 ... 21 22 23
0 接受大宗购买 112 112 ... 227 52 52
1 接受大宗卖出 1573.2 1575.2 ... 1833.8 1728.3 1649.3
2 拒绝块购买 NaN NaN ... NaN NaN NaN
3 拒绝大宗卖出 NaN NaN ... NaN NaN NaN
4 净进口 2652.3 2505.9 ... 2932 2962 2897
【问题讨论】:
-
print("## SQL: " + columnInsertSql)打印什么? -
这一行将打印出:INSERT INTO DataSets (Hour, BlockBuyNet, BlockSell, RejectedBlockBuy, RejectedBlockSell, NetImports) VALUES(0, 'Accepted Block Buy'), (1, 'Accepted Block Sell' ), (2, '拒绝大宗买入'), (3, '拒绝大宗卖出'), (4, '净进口'),
-
您的 INSERT 命名了 5 列,但您的 VALUES 子句仅指定了 2 个值。
-
好的,谢谢,你有什么想法我可以解决它吗?
标签: python sql-server excel pandas