无法使用QFileDialog.getSaveFileName另存为csv

时间:2018-12-18 21:41:59

标签: qfiledialog

df = pd.DataFrame({'col1': [1, 2], 'col2': [3, 4]})
fileName = QFileDialog.getSaveFileName(self,"Save",os.getcwd(),"CSV Files (*.csv)")
if fileName:
    with open(fileName, "w") as file:
        file.write(df)

我正在尝试使用QFileDialog而不是df.to_csv将数据帧保存到csv,但这不起作用

1 个答案:

答案 0 :(得分:0)

fileName = QFileDialog.getSaveFileName(self,"Save",os.getcwd(),"CSV Files (*.csv)")
print(fileName)

这将返回元组中的字符串,首先是路径

您应该写:

fileName, _ = QFileDialog.getSaveFileName(self,"Save",os.getcwd(),"CSV Files (*.csv)")