我有一个文件夹,其子文件夹堆叠着.xls文件,我想将它们合并到一个大型DataFrame中并将其导出到mssql服务器。此外,文件名包含一个时间戳ddmmmyyyy,我需要提取并连接到df。
import pandas as pd
import numpy as np
import os, pymssql, pyodbc
from datetime import datetime
from sqlalchemy import create_engine
def connect():
return pyodbc.connect(
r'DRIVER={SQL Server};'
r'SERVER=myServer;'
r'DATABASE=myDB;'
r'UID=myUser;'
r'PWD=myPwd;'
r'TDS_Version=7.3;'
r'Port=1337'
)
cnx = create_engine('mssql://', creator=connect)
cnx.connect()
# Parse files and dump to SQL
folder = "\myFolder\""
for root, dirs, files in os.walk(folder):
for file in files:
if file.endswith(".xls") and ("~" not in file):
df = pd.read_excel(root + "/" + file,header=5)
tmp = file.split("_")[2]
tmp = datetime.strptime(tmp, '%d%b%Y')
df['Created'] = tmp
df.to_sql(name="myTable", con=cnx, if_exists='append', index=False)
# Check the dumped content
sql = "SELECT * FROM myTable"
df = pd.read_sql(sql, cnx)
df.head()
连接有效,从我收集的循环运行开始,但没有新数据添加到DataFrame。 df.head()
返回一个未更改的表。有人对我做错了什么有任何线索?
此外,在运行create_engine语句时,我收到此恼人的连接警告,但它不会影响任何内容:
SAWarning:未指定驱动程序名称;这是PyODBC预计的时候 使用无DSN连接“未指定驱动程序名称;
任何帮助表示赞赏! :)