【发布时间】:2018-01-22 07:07:27
【问题描述】:
我是 Python 编程新手,正在寻求一些帮助/指导来纠正我的 Python 代码。
这是我的查询。
- 我有一个 Excel 文件,其中包含(7 个标签)。
- 我有一个文件夹,其中包含 7 个不同的文本文件,每个文本文件都包含各自的选项卡 SQL 查询,每个文本文件名与 Excel 文件中的选项卡名称相同。
我编写了一个 Python 代码来逐个循环遍历所有文本文件并执行每个文本文件 SQL 查询以及将输出的任何数据,输出数据应转储到相应工作表/选项卡中的现有 excel 文件中.我正在使用 pandas 来执行此操作,但是,代码运行良好,但是在将数据更新到 excel 时,pandas 正在从文件中删除所有现有工作表,并且仅将当前输出数据更新到 excel 文件中。
示例:如果 Python 代码执行一个文本文件(文件名:数据),在执行此 SQL 查询后,我们得到了一些数据,这些数据应转储到 excel 文件(工作表名:数据)中。
<pre><code>
import pypyodbc
import pandas as pd
import os
import ctypes
from pandas import ExcelWriter
fpath = r"C:\MNaveed\DataScience\Python Practice New\SQL Queries"
xlfile = r"C:\MNaveed\DataScience\Python Practice New\SQL Queries\Open_Case_Data.xlsx"
cnxn = pypyodbc.connect('Driver={SQL Server};Server=MyServerName;Database=MyDatabaseName;Trusted_Connection=Yes')
cursor = cnxn.cursor()
for subdir, dirs, files in os.walk(fpath):
for file in files:
#print(os.path.join(subdir,file))
filepath = os.path.join(subdir,file)
#print("FilePath: ", filepath)
if filepath.endswith(".txt"):
if file != "ClosedAging_Cont.txt":
txtdata = open(filepath, 'r')
script = txtdata.read().strip()
txtdata.close()
cursor.execute(script)
if file == "ClosedAging.txt":
txtdata = open(os.path.join(subdir,"ClosedAging_Cont.txt"), 'r')
script = txtdata.read().strip()
txtdata.close()
cursor.execute(script)
col = [desc[0] for desc in cursor.description]
data = cursor.fetchall()
df = pd.DataFrame(list(data),columns=col)
#save_xls(df,xlfile)
writer = pd.ExcelWriter(xlfile)
flnm = file.replace('.txt','').strip()
df.to_excel(writer,sheet_name=flnm,index=False)
writer.save()
print(file, " : Successfully Updated.")
else:
print(file, " : Ignoring this File")
else:
print(file, " : Ignoring this File")
ctypes.windll.user32.MessageBoxW(0,"Open Case Reporting Data Successfully Updated","Open Case Reporting",1)
</pre></code>
【问题讨论】:
-
据我所知,
xlsxwriter只是一名作家。它没有读取文件的能力,这意味着它无法查找您要附加数据的最后一行。您需要openpyxl来查找空行。然后使用xlsxwriter通过指定行来写入。或者您可以在openpyxl中重新编写您的代码 -
已格式化并修正错别字
-
嗨 Muthu Kumaran,我不是在寻找附加数据。我想将数据转储到现有工作表中(现有数据应首先删除并转储更新的数据)。但是这里熊猫每次循环时都不会创建新的数据框..