Home > Software design >  write or replace data in csv file using python
write or replace data in csv file using python

Time:09-09

Using this code I have write/replace data in an excel file. But now i need to do the same thing for csv file. Can someone help me with that?

with pd.ExcelWriter('2.xlsx', mode='a', engine='openpyxl', if_sheet_exists='replace') as writer:
fm_data.to_excel(writer, sheet_name='Raw Data1')

CodePudding user response:

ExcelWriter are used to write to excel files. If you are using pandas dataframes you can use to_csv instead.

df.to_csv('csvfilename.csv')

Here is is link to the relevant documentation for to_csv https://pandas.pydata.org/docs/reference/api/pandas.DataFrame.to_csv.html

If you are not using pandas, take a look at the csv package: https://docs.python.org/3/library/csv.html

CodePudding user response:

You can use pandas' to_csv() function:

df.to_csv('2.csv)
  • Related