简体   繁体   English

如何转换代码以在 xlsx 而不是 csv 中使用 python 而不使用 pandas 写入数据?

[英]How to convert code to write data in xlsx instead of csv using python without pandas?

I have written a python code to store data in csv format but now need to store in.xlsx file.我已经写了一个 python 代码来存储 csv 格式的数据,但现在需要存储在.xlsx 文件中。 how to convert the below code and write the output in.xlsx file.如何转换以下代码并写入 output in.xlsx 文件。

  details_file = self.get_local_storage_path() + '/Myfile.xlsx'
  csv_columns = (
        'id', 'name', 'place', 'salary', 'email',
          )

   with open(details_file, 'w') as csvfile:
            writer = csv.DictWriter(
                csvfile, fieldnames=csv_columns, extrasaction='ignore')
            writer.writeheader()
            for data in details:
                writer.writerow(data)
                
                
                
                

#I tried as below but getting error as TypeError: int() argument must be a string, a bytes-like object or a number, not 'dict' #I 试过如下但得到错误 TypeError: int() argument must be a string, a bytes-like object or a number, not 'dict'

with open(details_file, 'w') as csvfile:           
            print("inside open")
            workbook = xlsxwriter.Workbook(csvfile)
            worksheet = workbook.add_worksheet()
            print("before for loop")
            for data in details:
                worksheet.write(data)
            workbook.close()

I would use pandas for this:为此,我会使用pandas

import pandas as pd

# create the dataframe
df = pd.DataFrame(dict(zip(csv_columns, details)))

# save to csv:
df.to_csv(path, index=False)

#save to xlsx:
df.to_excel(path, index=False)

You might need to install openpyxl for the last line to work.您可能需要安装openpyxl才能使最后一行工作。

But if you really need to use xlsxwriter for this, here's how I would do it based on their tutorial :但是,如果您真的需要为此使用xlsxwriter ,那么根据他们的教程,我会这样做:

import xlsxwriter

csv_columns = (
    'id', 'name', 'place', 'salary', 'email',
)
details = [
    [1, 'A', 'B', 2, 'c@d.com'],
    [3, 'C', 'D', 4, 'e@f.com'],
]

workbook = xlsxwriter.Workbook(path)
worksheet = workbook.add_worksheet()

for col, name in enumerate(csv_columns):
    worksheet.write(0, col, name)

for row, det in enumerate(details, 1):
    for col, value in enumerate(det):
        worksheet.write(row, col, value)

workbook.close()

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

相关问题 如何使用Python Pandas将CSV文件写入XLSX? - How to write CSV files into XLSX using Python Pandas? Python 使用 pandas 将 xlsx 转换为 csv 文件。 如何删除索引列? - Python using pandas to convert xlsx to csv file. How to delete index column? 如何使用熊猫和地球将多个 .xlsx 文件转换为 .csv - How to convert multiple .xlsx files in to .csv using pandas and globe 如何使用pandas将目录中的多个.XLSX文件转换为CSV? - How to convert multiple .XLSX files in a diretory to CSV using pandas? Python 如何在不写入目录的情况下读取 xlsx 文件并转换为 csv - Python how to read xlsx file and convert into csv without writing to directory 在不使用 pandas 的情况下将字典转换为 python 中的 csv? - convert dictionary to csv in python without using pandas? 我需要使用 python 中的 pandas 拆分 csv 数据库中的数据,并将其写入具有多张工作表的 xlsx 文件,并创建一个额外的 col - I need to split data in a csv database using pandas in python and write it to and xlsx file with multiple sheets as well as creating an additional col 如何使用数据框熊猫在python中转换数据csv - how to convert data csv in python using dataframe pandas 如何使用python将多个xlsx表转换为csv - How to convert multiple xlsx sheet to csv using python Python pandas xlsx/ csv - Python pandas xlsx/ csv
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM