Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

python csv two columns with same name

Tags:

python

csv

Is there a way to use the python csv module to save a csv which has two columns of the same name?

This is my function

def DictListToCsv(Data, FileName, FieldNames):
    with open(FileName, 'w') as f:
        writer = csv.DictWriter(f, fieldnames = FieldNames, dialect = 'excel', delimiter=',', lineterminator='\n')
        writer.writeheader()
        writer.writerows(Data)

Every approach i try would involve at some point having a dictionary with two headers named the same, which is of course not possible

like image 280
teebagz Avatar asked Dec 12 '25 02:12

teebagz


1 Answers

Have you tried using pandas module ? Convert your data into pandas dataframe ("df") and then use :

df.columns=['x','x']

and finally, save it to a csv file :

 df.to_csv(r'/home/ishan/Desktop/a.csv',header=True, index=False)

it will work.

like image 108
Ishan Avatar answered Dec 13 '25 15:12

Ishan



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!