Aug-29-2023, 11:13 PM
(This post was last modified: Aug-30-2023, 09:57 AM by Larz60+.
Edit Reason: added code tags
)
Hi Team,
I am trying to Read xlsx file and I want to write it into CSV File with pipe delimited.all decimal values wanted to round up 6 decimal places.
Challenges I am facing for Negative Numbers
xlsx values
-2816151988.789466
in CSV File values are getting written as , negative symbol getting missed.
2816151988.789466
how to fix it.
Thanks
mg
I am trying to Read xlsx file and I want to write it into CSV File with pipe delimited.all decimal values wanted to round up 6 decimal places.
Challenges I am facing for Negative Numbers
xlsx values
-2816151988.789466
in CSV File values are getting written as , negative symbol getting missed.
2816151988.789466
how to fix it.
Thanks
mg
import os
import pandas as pd
# Specify the source directory containing the xlsx files
xlsx_directory = "path/to/source/directory"
# Specify the destination directory for saving the csv files
csv_destination = "path/to/destination/directory"
# Create the destination directory if it doesn't exist
if not os.path.exists(csv_destination):
os.makedirs(csv_destination)
# Get a list of all xlsx files in the source directory
xlsx_files = [f for f in os.listdir(xlsx_directory) if f.endswith('.xlsx')]
# Loop through the xlsx files
for xlsx_file in xlsx_files:
# Construct the full path of the xlsx file
xlsx_path = os.path.join(xlsx_directory, xlsx_file)
# Load the xlsx file into a pandas DataFrame
df = pd.read_excel(xlsx_path)
# Construct the path for the CSV file in the destination directory
csv_file = os.path.splitext(xlsx_file)[0] + '.csv'
csv_path = os.path.join(csv_destination, csv_file)
# Save the DataFrame as a CSV file with pipe delimiter
df.to_csv(csv_path, sep='|', index=False, float_format='%.6f')
print(f"Converted {xlsx_file} to {csv_file}")
