Home > Mobile >  How to solve the error while saving a dataframe to csv in python?
How to solve the error while saving a dataframe to csv in python?

Time:05-12

I was saving the dataframe as below

Target1_file.to_csv( r'C:\Users\para\PycharmProjects\pythonProject1\Data\data1\' str(File_T1_name) '.csv', index=True,header=True)

where

File_T1_name = str(time_.tm_mday)   str(time_.tm_mon)   str(time_.tm_year)   "_SQ12_TC12."   str(filter_index)   "T1_statistics"

I am getting following error:

Missing closing quote [']
',' or ')' expected

how to solve this error?

CodePudding user response:

Use f-strings instead of raw strings:

Target1_file.to_csv(f"C:\Users\para\PycharmProjects\pythonProject1\Data\data1\{File_T1_name}.csv", index=True,header=True)

where

File_T1_name = f"{time_.tm_mday}{time_.tm_mon}{time_.tm_year}_SQ12_TC12.{filter_index}T1_statistics"

CodePudding user response:

One thing you could tryout is adding ".csv" to file name variable itself

File_T1_name = str(time_.tm_mday)   str(time_.tm_mon)   str(time_.tm_year)   "_SQ12_TC12."   str(filter_index)   "T1_statistics"   ".csv"

and then use "\\"(double backslash) in the file path instead of "\"(single backslash)

Target1_file.to_csv('C:\\Users\\para\\PycharmProjects\\pythonProject1\\Data\data1\\'   str(File_T1_name), index=True, header=True)
  • Related