0

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.

1 Answer 1

1

Instead of

    writer.writeheader()

Fake a row of data:

    writer.writerow(dict(zip(name_keys,header_names)))
Sign up to request clarification or add additional context in comments.

1 Comment

thanks sir , worked for me

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.