Find answers from the community

s
F
Y
a
P
Updated last month

i want to use the simpledirectory reader

i want to use the simpledirectory reader and i have a csv but its ; delimited can i define this somehow ?
L
h
5 comments
i tried that.. 😦 ended up doing like this instead:

FIX CSV....

import os
import pandas as pd

Function to process a CSV file

def process_csv(file_path):
print("Processing:", file_path)

try:
df = pd.read_csv(file_path) # Read CSV into DataFrame
print("Delimiter is already ','.")
except:
df = pd.read_csv(file_path,delimiter=';') # Read CSV into DataFrame
print("Converting delimiter...")
df.to_csv(file_path, sep=',', index=False) # Write DataFrame with ',' delimiter
print("File converted and overwritten.")


Folder containing CSV files

folder_path = r'./data'

Iterate over each file in the folder

for filename in os.listdir(folder_path):
if filename.endswith('.csv'): # Check if file is CSV
file_path = os.path.join(folder_path, filename)
process_csv(file_path) # Process the CSV file
I'm confused why the custom reader didn't work. But that works too πŸ™‚
Iam just to much of a python newbie to solve it, i bet it works though
haha ok, fair enough πŸ™‚
Add a reply
Sign up and join the conversation on Discord