add encoding detection

This commit is contained in:
WorldTeacher
2024-07-02 07:40:09 +02:00
parent 7e0de3f032
commit ff7e6fcda3

View File

@@ -1,13 +1,13 @@
import csv import csv
import pandas as pdf import chardet
def csv_to_list(path: str) -> list[str]: def csv_to_list(path: str) -> list[str]:
""" """
Extracts the data from a csv file and returns it as a pandas dataframe Extracts the data from a csv file and returns it as a pandas dataframe
""" """
with open(path, newline="", encoding="utf-8") as csvfile: encoding = chardet.detect(open(path, "rb").read())["encoding"]
with open(path, newline="", encoding=encoding) as csvfile:
# if decoder fails to map, assign "" # if decoder fails to map, assign ""
reader = csv.reader(csvfile, delimiter=";", quotechar="|") reader = csv.reader(csvfile, delimiter=";", quotechar="|")
ret = [] ret = []