Skip to content

Commit

Permalink
issue #1178 - CSV quoting
Browse files Browse the repository at this point in the history
  • Loading branch information
davmlaw committed Oct 4, 2024
1 parent 100c498 commit f516e2c
Showing 1 changed file with 35 additions and 0 deletions.
35 changes: 35 additions & 0 deletions snpdb/management/commands/one_off_fix_csv_quoting.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
import csv
import os
import zipfile

from django.core.management.base import BaseCommand
from snpdb.models import CachedGeneratedFile


class Command(BaseCommand):
"""
"""
@staticmethod
def fix_csv_zip(zip_filename):
# Move the old one to a backup
backup_name = zip_filename + ".backup"
os.rename(zip_filename, backup_name)

with zipfile.ZipFile(zip_filename, 'r') as zip_file:
csv_filename = os.path.splitext(os.path.basename(zip_filename))[0]
with zip_file.open(csv_filename) as csv_file:
reader = csv.reader(csv_file.read().decode('utf-8').splitlines(), dialect='excel', escapechar='\\',
quoting=csv.QUOTE_NONE)

fixed_csv_filename = os.path.splitext(zip_filename)[0]
with open(fixed_csv_filename, "wt") as out_csv_file:
writer = csv.writer(out_csv_file, dialect='excel', quoting=csv.QUOTE_MINIMAL)
for r in reader:
writer.writerow(r)

def handle(self, *args, **options):
AFFECTED_GENERATORS = ["export_sample_to_downloadable_file", "export_cohort_to_downloadable_file"]
for cgf in CachedGeneratedFile.objects.filter(generator__in=AFFECTED_GENERATORS):
self.fix_csv_zip(cgf.filename)

0 comments on commit f516e2c

Please sign in to comment.