Skip to content

Commit

Permalink
fix non-ascii chars in csv using django.default_storage re archesproj…
Browse files Browse the repository at this point in the history
  • Loading branch information
whatisgalen committed Aug 15, 2024
1 parent 46d7c80 commit 24c4b3b
Showing 1 changed file with 7 additions and 6 deletions.
13 changes: 7 additions & 6 deletions arches/app/etl_modules/import_single_csv.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import csv
import io
from datetime import datetime
import json
import os
Expand Down Expand Up @@ -116,8 +117,9 @@ def read(self, request):
"message": _("Upload a valid csv file"),
}

with default_storage.open(csv_file_path, mode="r") as csvfile:
reader = csv.reader(csvfile)
with default_storage.open(self.csv_file_path, mode="rb") as csvfile:
text_wrapper = io.TextIOWrapper(csvfile, encoding="utf-8")
reader = csv.DictReader(text_wrapper)
data = {"csv": [line for line in reader], "csv_file": csv_file_name}
with connection.cursor() as cursor:
cursor.execute(
Expand Down Expand Up @@ -300,10 +302,9 @@ def populate_staging_table(
):
temp_dir = os.path.join(settings.UPLOADED_FILES_DIR, "tmp", loadid)
csv_file_path = os.path.join(temp_dir, csv_file_name)
with default_storage.open(csv_file_path, mode="r") as csvfile:
reader = csv.reader(
csvfile
) # if there is a duplicate field, DictReader will not work
with default_storage.open(csv_file_path, mode="rb") as csvfile:
text_wrapper = io.TextIOWrapper(csvfile, encoding="utf-8")
reader = csv.reader(text_wrapper)

if has_headers:
next(reader)
Expand Down

0 comments on commit 24c4b3b

Please sign in to comment.