'How to solve the error while saving a dataframe to csv in python?
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?
Solution 1:[1]
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)
Solution 2:[2]
Use f-strings instead of raw strings:
Target1_file.to_csv(fr"C:\Users\para\PycharmProjects\pythonProject1\Data\data1\{File_T1_name}.csv", index=True,header=True)
where
File_T1_name = fr"{time_.tm_mday}{time_.tm_mon}{time_.tm_year}_SQ12_TC12.{filter_index}T1_statistics"
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | Dany Bright |
| Solution 2 |
