我正在开发一个软件来处理公司的最后期限。我想把SQL表导出到excel文件的CSV。我读过这个问题如何使用python脚本导出Mysql表数据到excel文件?并尝试了提供的解决方案,但我遇到了一个问题。CSV文件跳过一行(每一行之间有一个空行,参见示例)。
rg,parti,oggetto,incombente,autorita,giudice,deadline_id,member_id,data,ora,minuti,notes,terminata
123,io,bo,a,ibi,prof,2,1,2022-11-13,10,0,adfggnadfnadf,0
123,io,bo,ia,ib,prof,3,1,2023-01-14,1,24,adfggnadfnadfadfggnadfnadf,0
1241426,sdfgn,ASDG,srtgnawetjn,hgdm,sry,4,1,2023-01-07,10,24,WQEGOUIB<IUSDBV,0
124512,wrtj,SADG,tgjw,rtyj,sfgjh,5,1,2023-01-07,10,31,srgoibn,0
代码如下:
cursor.execute("SELECT * FROM `lawsuit` INNER JOIN `calendar` WHERE lawsuit.rg = calendar.rg;")
result = cursor.fetchall()
print(result)
def save_file():
file_path = asksaveasfile(initialfile='Untitled.csv',
defaultextension=".csv",
filetypes=[("CSV file", "*.csv")])
if file_path:
writer = csv.writer(file_path)
writer.writerow(result[0].keys())
for row in result:
print(row)
writer.writerow(row.values())
else:
messagebox.showerror("NO FILE SELECTED", "You have cancelled the action, the file has not been created")
btn = tk.Button(self.root, text="Export to CSV", command=lambda: save_file())
btn.place(x=10, y=10)
为了简化每个人的生活,我将简单地附加在这里打印result
变量的结果(这样我们就不需要使用SQL表了。)
[{'rg': 123, 'parti': 'io', 'oggetto': 'bo', 'incombente': 'a', 'autorita': 'ibi', 'giudice': 'prof', 'deadline_id': 2, 'member_id': 1, 'data': datetime.date(2022, 11, 13), 'ora': 10, 'minuti': 0, 'notes': 'adfggnadfnadf', 'terminata': 0}, {'rg': 123, 'parti': 'io', 'oggetto': 'bo', 'incombente': 'ia', 'autorita': 'ib', 'giudice': 'prof', 'deadline_id': 3, 'member_id': 1, 'data': datetime.date(2023, 1, 14), 'ora': 1, 'minuti': 24, 'notes': 'adfggnadfnadfadfggnadfnadf', 'terminata': 0}, {'rg': 1241426, 'parti': 'sdfgn', 'oggetto': 'ASDG', 'incombente': 'srtgnawetjn', 'autorita': 'hgdm', 'giudice': 'sry', 'deadline_id': 4, 'member_id': 1, 'data': datetime.date(2023, 1, 7), 'ora': 10, 'minuti': 24, 'notes': 'WQEGOUIB<IUSDBV', 'terminata': 0}, {'rg': 124512, 'parti': 'wrtj', 'oggetto': 'SADG', 'incombente': 'tgjw', 'autorita': 'rtyj', 'giudice': 'sfgjh', 'deadline_id': 5, 'member_id': 1, 'data': datetime.date(2023, 1, 7), 'ora': 10, 'minuti': 31, 'notes': 'srgoibn', 'terminata': 0}, {'rg': 12453425, 'parti': 'arhnadfn', 'oggetto': 'sdfna', 'incombente': 'aedrh', 'autorita': 'sdfgn', 'giudice': 'aetn', 'deadline_id': 6, 'member_id': 1, 'data': datetime.date(2023, 1, 7), 'ora': 10, 'minuti': 30, 'notes': 'enqefnadfnnaethnadf', 'terminata': 0}]
遵循@Adrian Klaver评论的想法,问题可能存在于asksaveasfile
函数中。如果我打印file_path变量,我会得到:name='C:/Users/serax/OneDrive/Documenti/a.csv' mode='w' encoding='cp1252'>
.
这是一个io.TextIoWrapper
(https://docs.python.org/3/library/io.html#io.TextIOWrapper),在写模式下有一些奇怪的编码。如果我设法从这个io.TextIoWrapper
对象中检索文件的路径,那么我可以用标准open()
函数打开它,希望它能工作。
任何帮助都是非常感激的!;)
解决了!: D
cursor.execute("SELECT * FROM `lawsuit` INNER JOIN `calendar` WHERE lawsuit.rg = calendar.rg;")
result = cursor.fetchall()
print(result)
file_path = asksaveasfilename(initialfile='Untitled.csv',
defaultextension=".csv",
filetypes=[("CSV file", "*.csv")])
print(file_path)
if file_path:
with open(file_path, "w", newline="") as file:
writer = csv.writer(file)
writer.writerow(result[0].keys())
for row in result:
print(row)
writer.writerow(row.values())
else:
messagebox.showerror("NO FILE SELECTED", "You have cancelled the action, the file has not been created")
btn = tk.Button(self.root, text="Export to CSV", command=lambda: save_file())
btn.place(x=10, y=10)
没有使用返回Io
对象的asksavefile
,而是使用返回新创建文件路径的asksavefilename
。然后我只是正常打开它,并将newline=""
添加到open()
。文件现在不留下任何空行了!