Skip to content

Instantly share code, notes, and snippets.

@blackrobot
Created March 3, 2023 19:04
Show Gist options
  • Save blackrobot/129fcef482387471e92b4f77eba58992 to your computer and use it in GitHub Desktop.
Save blackrobot/129fcef482387471e92b4f77eba58992 to your computer and use it in GitHub Desktop.
Remove broken quoting in CSV exports from ZenHub
#!/usr/bin/env python3
"""
Read a CSV file from stdin and write a fixed CSV file to stdout.
Example:
$ cat zenhub.csv | ./fix-zenhub-csv.py > fixed.csv
$ fix-zenhub-csv.py zenhub.csv > fixed.csv
"""
import csv
import fileinput
import functools
import sys
from rich import get_console
console = get_console()
def read_csv_stream() -> list[str]:
# https://docs.python.org/3/library/fileinput.html
with fileinput.input(encoding="utf-8-sig") as fobj:
reader = csv.reader(fobj)
yield from reader
def fix_line_commas(row: list[str]) -> list[str]:
""" Correct the unescaped commas in the "Issue Title" column
1. Count 4 commas from end of line
2. Count 1 comma from start of line
3. Fix the quotes between those two markers
"""
# NOTE: This strips all quotes from the issue title
issue_title = "".join(row[1:-4]).replace('"', "")
return [*row[0:1], issue_title, *row[-4:]]
@functools.cache
def stdout_csv_writer() -> csv.writer:
return csv.writer(sys.stdout, dialect="excel")
def write_csv_stream(row: list[str]) -> None:
stdout_csv_writer().writerow(row)
def main() -> None:
for row in read_csv_stream():
write_csv_stream(fix_line_commas(row))
if __name__ == "__main__":
args = sys.argv[1:]
if any(arg.lower() in ("-h", "--help") for arg in args):
console.print(__doc__)
sys.exit(0)
main()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment