Skip to content

Instantly share code, notes, and snippets.

@tjarksaul
Last active January 16, 2021 16:44
Show Gist options
  • Save tjarksaul/8339387b24cfa8460c11f2ad02fae858 to your computer and use it in GitHub Desktop.
Save tjarksaul/8339387b24cfa8460c11f2ad02fae858 to your computer and use it in GitHub Desktop.
#!/usr/bin/env python3
import sys
import os
import csv
import xlsxwriter # pip3 install xlsxwriter
from typing import List
def convert_row(row: List[str]) -> List[str]:
row[3] = convert_gender(row[3])
return row
def convert_gender(gender: str) -> str:
return "m" if gender == "Herr" else "w"
def convert(infile: str, outfile: str) -> None:
rows = get_converted_rows(infile)
write_converted_rows(outfile, rows)
def get_converted_rows(infile: str) -> List[List[str]]:
rows = []
with open(infile, newline='', encoding='windows-1252') as csvfile:
reader = csv.reader(csvfile, delimiter=';')
rows.append(next(reader))
for row in reader:
new_row = convert_row(row)
rows.append(new_row)
return rows
def write_converted_rows(outfile: str, rows: List[List[str]]):
with xlsxwriter.Workbook(outfile) as workbook:
worksheet = workbook.add_worksheet()
for i, row in enumerate(rows):
for j, val in enumerate(row):
worksheet.write(i, j, val)
def main() -> None:
if not len(sys.argv) == 3 or not os.path.isfile(sys.argv[1]):
print(f'Usage: {sys.argv[0]} infile outfile')
exit(1)
infile = sys.argv[1]
outfile = sys.argv[2]
convert(infile, outfile)
if __name__ == "__main__":
main()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment