[Answer]-Python CSV, adding or changing column data

1👍

If you open the output file as a DictWriter() object, then you can write out your altered dictionaries quite easily. You do need to determine your extra fieldnames ahead of time:

with open(os.path.join(OUTPUT_DIR, f), 'rb') as rfile:
    reader = csv.DictReader(rfile)
    headers = reader.fieldnames 

    rules = Substring.objects.filter(source_file=f).all()

    # pre-process the rules to determine the headers
    for rule in rules:
        from_column = rule.from_column
        to_column = rule.to_column

        if from_column not in headers:
            # problem; perhaps raise an error?
        if to_column not in headers:
            headers.append(to_column

    with open(os.path.join(DZINE_DIR, f), "wb") as wfile:
        writer = csv.DictWriter(wfile, fieldnames=headers)
        for row in reader:
            for rule in rules:
                from_column = rule.from_column
                to_column = rule.to_column

                if rule.get_rule_type_display() == "substring":
                    string = rule.string.split(",")
                    row[to_column] = string[0] + row[from_column] + string[1]

            writer.writerow(reader)

Leave a comment