41 lines
1.3 KiB
Python
41 lines
1.3 KiB
Python
import pandas as pd
|
|
import chardet
|
|
|
|
|
|
def check_file(path):
|
|
with open(path, 'rb') as file:
|
|
result = chardet.detect(file.read())
|
|
|
|
detected_encoding = result['encoding']
|
|
try:
|
|
pd.read_csv(path, encoding=detected_encoding)
|
|
except pd.errors.ParserError as e:
|
|
# Wenn ein Parserfehler auftritt, gibt eine Fehlermeldung aus
|
|
print(f"Fehler beim Einlesen der CSV-Datei: {e}")
|
|
print()
|
|
data = open(path, "r")
|
|
data = ''.join([i for i in data]).replace(",", "")
|
|
x = open(path, "w")
|
|
x.writelines(data)
|
|
x.close()
|
|
print(f"Alle Kommas entfernt")
|
|
|
|
|
|
# Prüft Formatierung der CSV, formatiert diese zu utf-8 und speichert das Ergebnis als neue Liste
|
|
def format_csv(path, type):
|
|
with open(path, 'rb') as file:
|
|
result = chardet.detect(file.read())
|
|
|
|
detected_encoding = result['encoding']
|
|
|
|
# CSV-Datei mit Pandas einlesen
|
|
try:
|
|
df = pd.read_csv(path, encoding=detected_encoding)
|
|
print("Datei erfolgreich eingelesen.")
|
|
df.to_csv(type, index=False, encoding='utf-8')
|
|
print("UTF-8 Kopie erfolgreich erstellt.")
|
|
|
|
except pd.errors.ParserError as e:
|
|
# Wenn ein Parserfehler auftritt, gibt eine Fehlermeldung aus
|
|
print(f"Fehler beim Einlesen der CSV-Datei: {e}")
|