Home > Net >  change header of csv python
change header of csv python

Time:02-17

hi I am writing data on csv using python my code is something like this

name_keys =['Record_1', 'Record_2', 'Random_Name'] 

header_names =  ['Record 1', 'Record 2', 'Random Name'] 

with open(file_path, 'w', encoding='UTF8', newline='') as f:
    writer = csv.DictWriter(f, fieldnames=name_keys)
    writer.writeheader()
    writer.writerows(data)

issue is my header_names are suppose to be bit diff from name_keys . so how I can achieve this ? And I don't want to use pandas here.

CodePudding user response:

Instead of

    writer.writeheader()

Fake a row of data:

    writer.writerow(dict(zip(name_keys,header_names)))
  • Related