import xlrd
book = xlrd.open_workbook("samples.xlsx")
sheet_names = book.sheet_names()
for sheet_name in sheet_names:
sheet = book.sheet_by_name(sheet_name)
recs = ",".join(["('{0[0]}', {0[1]})".format(sheet.row_values(i)) for i in range(sheet.nrows)])
insert_sql = f"insert into {sheet_name} (name, age) values {recs};"
print(insert_sql)
# Connect to execute the insert data sql via pyodbc, pymssql or others
import xlrd
import pandas as pd
file_name = "samples.xlsx"
col_names = ["name", "age"]
book = xlrd.open_workbook(file_name)
sheet_names = book.sheet_names()
for sheet_name in sheet_names:
df = pd.read_excel(file_name, sheet_name=sheet_name, index_col=None, header=None, names=col_names)
print(df)
# Use df.to_sql with SQLAlchemy connection, that you can do it by yourself via refer to the pandas and SQLAlchemy documents.
例如,我创建了一个包含两张工作表的excel文件,如下图所示。你知道吗
有两种解决方案可以满足您将数据从excel表传输到sqlazure表的需要。你知道吗
通过^{} (its document)读取echo sheet的数据生成insert sql,然后通过^{} 、^{} 或其他方式执行这些insert sql。下面是我用Python从excel表读取数据的示例代码。你知道吗
以上代码的结果是。你知道吗
同时,请参考Azure官方文档^{} 和^{} ,了解如何使用pyodbc或pymssql连接和查询Azure SQL数据库。
您可以通过} 将数据写入sqlazure。下面是我的示例代码,用于读取excel文件的工作表数据以获取数据框。你知道吗
pandas
读取excel文件的表数据,然后通过SQLAlchemy连接通过函数^{以上代码的结果是。你知道吗
要通过SQLAlchemy连接sqlazure,请参阅SQLAlchemy文档^{} 和其他SO线程Connecting to an Azure database using SQLAlchemy in Python。
希望有帮助。你知道吗
相关问题 更多 >
编程相关推荐