简体   繁体   中英

Converting CSV file to .xlsx file

I am trying to convert a CSV file to a.xlsx file, where the source CSV file is saved on my Desktop. I want the output file to be saved to my Desktop.

I have tried the below code. However, I am getting a 'file not found' error and 'create the parser' error. I do not know what these errors mean.

I seek:

  1. Help to fix the script and
  2. Help understanding the causes of the problem.

import pandas as pd

read_file = pd.read_csv(r'C:\Users\anthonyedwards\Desktop\credit_card_input_data.csv')
read_file.to_excel(r'C:\Users\anthonyedwards\Desktop\credit_card_output_data.xlsx', index = None, header=True)

Here's an example using xlsxwriter :

import os
import glob
import csv
from xlsxwriter.workbook import Workbook


for csvfile in glob.glob(os.path.join('.', 'file.csv')):
    workbook = Workbook(csvfile[:-4] + '.xlsx')
    worksheet = workbook.add_worksheet()
    with open(csvfile, 'rt', encoding='utf8') as f:
        reader = csv.reader(f)
        for r, row in enumerate(reader):
            for c, col in enumerate(row):
                worksheet.write(r, c, col)
    workbook.close()

FYI, there is also a package called openpyxl , that can read/write Excel 2007 xlsx/xlsm files.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM