fix(csvutils): drop sniffer code

This seems to break certain CSVs, which the default excel dialect can handle fine

Signed-off-by: Akhil Narang <me@akhilnarang.dev>
This commit is contained in:
Akhil Narang 2025-01-14 17:19:13 +05:30
parent c72e91f465
commit d9b9940a79
No known key found for this signature in database
GPG key ID: 9DCC61E211BF645F

View file

@ -2,7 +2,6 @@
# License: MIT. See LICENSE
import csv
import json
from csv import Sniffer
from io import StringIO
import requests
@ -58,28 +57,9 @@ def read_csv_content(fcontent):
fcontent = fcontent.encode("utf-8")
content = [frappe.safe_decode(line) for line in fcontent.splitlines(True)]
sniffer = Sniffer()
# Don't need to use whole csv, if more than 20 rows, use just first 20
sample_content = content[:20] if len(content) > 20 else content
# only testing for most common delimiter types, this later can be extended
# init default dialect, to avoid lint errors
dialect = csv.get_dialect("excel")
try:
# csv by default uses excel dialect, which is not always correct
dialect = sniffer.sniff(sample="\n".join(sample_content), delimiters=frappe.flags.delimiter_options)
except csv.Error:
# if sniff fails, show alert on user interface. Fall back to use default dialect (excel)
frappe.msgprint(
_(
"Delimiter detection failed. Try to enable custom delimiters and adjust the delimiter options as per your data."
),
indicator="orange",
alert=True,
)
try:
rows = []
for row in csv.reader(content, dialect=dialect):
for row in csv.reader(content):
r = []
for val in row:
# decode everything