1

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)
2
  • Have you checked that your Desktop is actually at C:\Users\anthonyedwards\Desktop? OneDrive for example relocates it. Your code is fine otherwise, so it really just is the file probably not really being there exactly. Commented Oct 9, 2020 at 3:14
  • I used the pwd command which returned '/Users/anthonyedwards' and then the file is saved on my Desktop. Is there any other way to check the file pathway? Commented Oct 9, 2020 at 3:23

1 Answer 1

3

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.

Sign up to request clarification or add additional context in comments.

3 Comments

These are alternative ways to achieve the same as OP's code, but don't address their problem (which is that the file simply isn't found)
I am very new to Python. I will do some research so I can understand the suggested script, but does anyone know the source of my specific problem / how I can find the correct file path?
Did you tried replacing the single backslash "\" with double backslashes "\\" in the file path?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.