简体   繁体   English

如何使用Python将数据从MySQL数据库保存到Excel文件而不是数字

[英]How do I save data from MySQL db to excel file as Number not text using Python

Below is the script. 下面是脚本。 I am pulling some data and save to Excel In the output file, all column are marked "Number stored as Text" What can I do in the script to change all columns to number (int)? 我要提取一些数据并保存到Excel中在输出文件中,所有列都标记为“数字存储为文本”。在脚本中我该怎么做才能将所有列更改为数字(int)? Thanks 谢谢

wb = openpyxl.Workbook()


db = MySQLdb.connect(host="...",port=...,user="...",passwd="...",db='...')
cursor = db.cursor()

cursor.execute("""
SELECT shopid,userid,count(orderid)as no_of_orders
from order_viw
group by shopid,userid
""")

data = cursor.fetchall()

rowNum = 2
col = 1
sheet = wb.get_sheet_by_name("Sheet")

for shopid,userid,no_of_orders in data:
    sheet.cell(column=col, row=rowNum, value="%d" % shopid)
    sheet.cell(column=col+1, row=rowNum, value="%d" % userid)
    sheet.cell(column=col+2, row=rowNum, value="%d" % no_of_orders)
    rowNum += 1
wb.save('C:/example.xlsx')
db.close()

As a forewarning, I'm running on Linux, have not got a MySql database and I am using libreoffice to check the results. 作为警告,我正在Linux上运行,还没有MySql数据库,并且我使用libreoffice检查结果。
With those caveats in place, the following looks like it works for me: 有了这些警告之后,以下内容似乎对我有用:

import openpyxl
wb = openpyxl.Workbook()
rowNum = 2
col = 1
sheet = wb.get_sheet_by_name("Sheet")
#data = [[42,1,43],[82,2,28]]
data = [['42','1','43'],['82','2','28']]
for shopid,userid,no_of_orders in data:
    sheet.cell(column=col, row=rowNum).value=int(shopid)
    sheet.cell(column=col+1, row=rowNum).value=int(userid)
    sheet.cell(column=col+2, row=rowNum).value=int(no_of_orders)
    rowNum += 1
wb.save('example.xlsx')

结果

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何使用Python将MySQL数据库数据写入Excel文件? - How to write MySQL DB data to Excel File using Python? 如何使用 Python 从多个文本文件中提取数据到 Excel? (每张纸一个文件的数据) - How do I extract data from multiple text files to Excel using Python? (One file's data per sheet) 如何将数据保存在文本文件python中 - How do I save data in a text file python 如何使用 python 仅将 URL 中的特定文本行保存到 txt 文件? - How do I save only specific lines of text from a URL to a txt file using python? 如何使用 Python 自动将数据写入 Excel 文件? - How do I write data into an Excel file automatically using Python? 如何使用 openpyxl / pandas 或任何 python 将从几个 excel 工作表中提取的字符串数据保存到新工作簿? - How do I save the string data I have extracted from several excel sheets to a new workbook using openpyxl / pandas or anything python? Python / xldr:如何使用raw_input变量从excel文件中提取数据? - Python / xldr : How do I pull data from an excel file using a raw_input variable? 如何保存文本文件中的特定数据并使用 python 保存结果数据 - How to save a specific data from a text file and save that resultant data using python 如何使用 python 或 JavaScript 提取文本并保存为 excel 文件 - How to extract text and save as excel file using python or JavaScript 如何使用 python 将文本文件拆分为行和列并保存在 excel 中 - How to split the text file into rows and columns and save in excel using python
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM