Home > database >  How to write Dynamic Values of Dictionary into csv python
How to write Dynamic Values of Dictionary into csv python

Time:12-03

I have a Dictionary where Keys are static and Values are Dynamic. I want to add those values into CSV file.

Input is given below :-

dictionary=[{
Name: f"{name of the person}",
Age: f"{Age of the person}",
Place:f"{place name}"
}]

Field names are given below

field_name=["Name","Age","Place"]

I want the csv like :

Name age place
A     1   Avc
B     2   Bds
C     3   Vsd
...   .. ....
...   .. ....
...   .. ....

But What I am getting is

Name age place
A     1   Avc
Name age place
B     2   Bds
Name age place
C     3   Vsd
Name age place
...  ..  ....
Name age place
...  ..  ....

My code to operate the csv is shown below.

with open("csv_can.csv","a",newline="") as f:
        writ= csv.DictWriter(f,fieldnames=field_names)
        writ.writeheader()
        writ.writerows(dictionary)

dictionary cointain Key:value pair and field_names contain Keys. This code is running in a while True loop

CodePudding user response:

You are appending (by using ...th open("csv_can.c...

  • Related