How to Add an Extra Column While Creating a CSV file with Python?

Viewed 27

I have a code like this:

with data_from_db(vars(args), sql_query) as row_source:
  build_csv(csv_filename, row_source)

Which call the following function:

def build_csv(filename, row_source):
  with open(filename, "w", newline="", encoding="utf-8") as csv_file:
    fixed = 'PINCARD'
    writer = csv.writer(csv_file, delimiter=";")
    for row in row_source:
        writer.writerow(row)

This code will get data from a database (query is saved on a external file) and will create a CSV file with two columns. However, I need to add an extra column with a fixed value on every single row, and that column must be the first one.

The current output is:

value1;value2

I need

PINCARD;value1;value2

However, I cannot figure out how to do that. Sorry for my lack of knowledge.

0 Answers
Related