我有一个程序,该程序可以读取电子表格并进行一些操作,然后保存配置文件,以便您可以恢复到以后设置的相同格式。配置文件正在保存为csv。
我遇到了一个问题,即一个数据集中的一列中包含逗号,这会破坏事情。因此,我使用to_csv和read_csv的“ sep”参数来避免该问题,但仍然会发生。
这是引起问题的列名: ['选择,计算或滑块标签']
我使用了'sep ='|'读写csv文件,但问题仍然存在。
谢谢!
这是代码...
def fxThree_A(self):
# function to save column choices to file
# get name for configuration from user
#
# save values from list of active columns to .csv file
self.config_df = pd.DataFrame(columns = ['config_name','active_columns', 'inactive_columns'])
self.config_df.loc[0,'config_name'] = 'config_one'
self.config_df.loc[0,'active_columns'] = ','.join(self.columns_active)
self.config_df.loc[0,'inactive_columns'] = ','.join(self.columns_inactive)
self.config_df.to_csv(self.rootFolder + '__config.csv', sep = '|', index=False)
print(self.columns_inactive)
def fxThree_B(self):
# function to read column choices from file
# read in the config file
self.config_df = pd.read_csv(self.rootFolder + '__config.csv', sep = '|',)#, encoding='ISO-8859-1')
# display list of preset configurations
# pick which config to use from dropwdown
# apply stored settings to current columns
self.columns_active = self.config_df.loc[0,'active_columns'].split(",")
self.columns_inactive = self.config_df.loc[0,'inactive_columns'].split(",")
# change button colors to match the new values
for button in self.column_buttons:
if button.cget("text") in self.columns_active:
button.configure(bg = "light green")
elif button.cget("text") in self.columns_inactive:
button.configure(bg = "red")
# catch any buttons not in the list for some reason, i.e. if the file format changed since last time the config was saved
else:
button.configure(bg = "orange")
tk.messagebox.showerror("Alert", "File format changed since configuration was last saved. Please re-select columns and re-save your configuration.")
答案 0 :(得分:0)
老兄。
这不是文件中的分隔符,而是我将列表连接成字符串的方式,以便将列表写成表的一个单元格。
我将此行中的','更改为'||',就解决了问题。
let next = self.storyboard?.instantiateViewController(withIdentifier: "NextVC") as! NextVC
next.onDismiss = { [weak self] (sendingData) in
self?.calendarView.receiveValue = sendingData
}
self.present(next, animated: true, completion: nil)
有时发布该问题会引起片刻的澄清:)
我应该把这个留给别人吗?