Skip to content

Instantly share code, notes, and snippets.

@zacharysyoung
Last active November 1, 2022 17:09
Show Gist options
  • Save zacharysyoung/49c267c56f33482bdb7c79a947d8ba6c to your computer and use it in GitHub Desktop.
Save zacharysyoung/49c267c56f33482bdb7c79a947d8ba6c to your computer and use it in GitHub Desktop.
A suggestion for SO-74269825

I want to suggest that you first combine all your CSVs into a single CSV, make sure that's correct, then convert the single CSV to XML:

  1. you'll be able to verify the intermediate, combined result
  2. issues like overwriting data simply disappear when any file is only being written to once

This takes more lines of code, and I find it's easier to get correct:

import csv

Combined_CSV = "combined.csv"

# I'm not sure how you get your final list of CSV paths and file names
csv_fnames = ["input1.csv", "input2.csv"]

all_rows = []
for csv_fname in csv_fnames:
    with open(csv_fname, newline="") as f:
        reader = csv.reader(f)
        next(reader)  # Skip header
        all_rows.extend(list(reader))

with open(Combined_CSV, "w", newline="") as f:
    writer = csv.writer(f)
    writer.writerows(all_rows)


# Take a moment, verify all the data looks good, pat yourself on the
# back, comment-out the following two lines, move on to converting
# the data to XML
print(f"combined all rows; inspect {Combined_CSV}")
sys.exit(0)


all_rows = []
with open(Combined_CSV, newline="") as f:
    reader = csv.reader(f)
    all_rows = list(reader)

with open("Client.xml", "w") as f:
    f.write("\n".join([convert_row(row) for row in all_rows]))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment