Skip to content

Commit 46b2805

Browse files
committed
import_csv: cannot sniff? try default dialects
1 parent b6c15de commit 46b2805

File tree

1 file changed

+5
-2
lines changed

1 file changed

+5
-2
lines changed

datasources/upload/import_csv.py

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -78,7 +78,10 @@ def process(self):
7878
# guess and set the properties as defined in import_formats.py
7979
infile = temp_file.open("r", encoding=encoding)
8080
sample = infile.read(1024 * 1024)
81-
possible_dialects = [csv.Sniffer().sniff(sample, delimiters=(",", ";", "\t"))]
81+
try:
82+
possible_dialects = [csv.Sniffer().sniff(sample, delimiters=(",", ";", "\t"))]
83+
except csv.Error:
84+
possible_dialects = csv.list_dialects()
8285
if tool_format.get("csv_dialect", {}):
8386
# Known dialects are defined in import_formats.py
8487
dialect = csv.Sniffer().sniff(sample, delimiters=(",", ";", "\t"))
@@ -90,7 +93,7 @@ def process(self):
9093
# With validated csvs, save as is but make sure the raw file is sorted
9194
infile.seek(0)
9295
dialect = possible_dialects.pop() # Use the last dialect first
93-
self.dataset.log(f"Importing CSV file with dialect: {vars(dialect)}")
96+
self.dataset.log(f"Importing CSV file with dialect: {vars(dialect) if type(dialect) == csv.Dialect else dialect}")
9497
reader = csv.DictReader(infile, dialect=dialect)
9598

9699
if tool_format.get("columns") and not tool_format.get("allow_user_mapping") and set(reader.fieldnames) & \

0 commit comments

Comments
 (0)