From 62f8fc3b5696fe45c146fecf5f8d0f816616f839 Mon Sep 17 00:00:00 2001 From: Jan Wille Date: Tue, 12 Jul 2022 16:46:25 +0200 Subject: [PATCH] more comments --- csv2xlsx.py | 30 ++++++++++++++++++++++-------- 1 file changed, 22 insertions(+), 8 deletions(-) diff --git a/csv2xlsx.py b/csv2xlsx.py index d4e03c9..45610aa 100644 --- a/csv2xlsx.py +++ b/csv2xlsx.py @@ -33,35 +33,49 @@ def main(glob_pattern, outdir, separator): spinner = cycle(["-", "/", "|", "\\"]) # a spinner to dislay "work in progress" + # loop over all files matching the glob pattern for csvfile in glob(glob_pattern): + + # handle output dir: outdir = path.join(path.dirname(csvfile), outdir) makedirs(outdir, exist_ok=True) # ensure outdir exists + + # create workbook object: xlsxfile = path.join(outdir, path.basename(csvfile)[:-4] + ".xlsx") workbook = Workbook(xlsxfile) worksheet = workbook.add_worksheet() - r, c = 0, -1 - with open(csvfile, "rt", encoding="utf8") as fp: + + r = 0 + c = -1 # we start at c=-1 because the first action is to increment c to 0 + with open(csvfile, "r") as fp: for line in fp.readlines(): + # remove newline characters from the end of the line: line = line.strip("\n") + + # loop over all the elements in a line: for token in line.split(separator): - c += 1 - token = token.strip(" \t") + c += 1 # first thing we do is increment so we can skip empty tokens + token = token.strip(" \t") # remove uneeded whitespace if not token: - continue + continue # skip empty tokens + + # escaped "string" tokens are written as is: if token.startswith('"'): token = token.strip('"') worksheet.write(r, c, token) + + # all not-strings are assumed to be numbers: else: if token == "nan": - continue + continue # skip nan's try: worksheet.write_number(r, c, float(token)) except ValueError: # Non-numbers that are not escaped with " worksheet.write(r, c, token) - c = -1 r += 1 - print("\r" + next(spinner), end="") + c = -1 + print("\r" + next(spinner), end="") # display "work in progress" try: workbook.close() print(f"\rconverted: {csvfile} -> {xlsxfile}")