0

I want to append data into multiple columns of a csv file at a time.

I have defined a function "writeCSV" for it which is taking col_name and col_value to append in csv file.

def writeCSV(col_name,col_data):

    with open('COMPONENT_DETAILS.csv',"a") as COMPONENT_DETAILS_file:

        fieldnames = ['ID','module_name','module_id','tech_name','component_name','connecion','counter','connection_type']
        csv_writer = csv.DictWriter(COMPONENT_DETAILS_file,fieldnames=fieldnames)
        csv_writer.writerow({col_name:col_data})

Now, suppose I want to append data into columns- tech_name, component_name,connection_type, how should I pass the argument to writeCSV() function? Would dictionary be helpful here?

Right now, I can only append to only one column by calling function with below syntax,

writeCSV('tech_name': 'Python')
Ankit Kumar
  • 107
  • 1
  • 3
  • 14
  • "Right now, I can only append to only one column by calling function with below syntax," You cannot, that is invalid syntax. – MisterMiyagi Feb 22 '19 at 06:22
  • Possible duplicate of [What is the purpose and use of \*\*kwargs?](https://stackoverflow.com/questions/1769403/what-is-the-purpose-and-use-of-kwargs) – MisterMiyagi Feb 22 '19 at 06:23
  • oh yeah..actually I was calling it with, writeCSV('tech_name','Python') Thanks for your reply MisterMiyagi!! – Ankit Kumar Feb 22 '19 at 09:25

1 Answers1

1
def writeCSV(dictionary):
        ##Your code
        csv_writer.writerow(dictionary)

writeCSV({'tech_name': 'Python2','component_name': 'Python3'})

This should do your work.As you have defined a DictWriter for writing to csv, you will have no problem in implementing this method.

Justice_Lords
  • 949
  • 5
  • 14