Skip to main content
Question

Getting “bot Error” for Python code. While i Convert CSV file to Excel using Python Script

  • 18 August 2023
  • 2 replies
  • 152 views

Getting “bot Error” for Code.

 

Steps:

  1. Convert to .py File.
  2. Open using A360 Commad
  3. Use Execute Function and call the function  “process_csv_to_excel”
  4. Closed Session

Code:

import pandas as pd
import openpyxl
import os

def process_csv_to_excel():
    csv_file_path = "C:/Automation/Test.csv"
    excel_file_path = "C:/Automation/Test.xlsx"
    
    try:
        # Read the CSV file
        read_file = pd.read_csv(csv_file_path, on_bad_lines='skip', encoding='utf-8')

        # Write to Excel
        read_file.to_excel(excel_file_path, index=None, header=True)

        success_message = "Conversion successful."
    except Exception as e:
        success_message = f"An error occurred: {str(e)}"

2 replies

Badge +1

I’m trying to solve a similar issue. 

Have you found the solution for it?

Userlevel 4
Badge +7

ChatGPT says there are a few errors with the code, and here is some corrected code:

  1. You're using the openpyxl library for Excel handling, but you haven't imported it, and you don't need it for the current task since pandas can directly write DataFrames to Excel.

  2. The variable success_message is assigned but not used.

import pandas as pd

def process_csv_to_excel():
csv_file_path = "C:/Automation/Test.csv"
excel_file_path = "C:/Automation/Test.xlsx"

try:
# Read the CSV file
read_file = pd.read_csv(csv_file_path, on_bad_lines='skip', encoding='utf-8')

# Write to Excel
read_file.to_excel(excel_file_path, index=None, header=True)

success_message = "Conversion successful."
except Exception as e:
success_message = f"An error occurred: {str(e)}"

return success_message

In this corrected version:

  1. I removed the unnecessary import openpyxl and import os since they are not used for this task.

  2. The variable success_message is now returned at the end of the function, making it available for use after calling the function.

Make sure to replace the file paths "C:/Automation/Test.csv" and "C:/Automation/Test.xlsx" with your actual file paths.

Reply