导出为csv完全适合第一个单元格

时间:2018-03-13 15:13:24

标签: python-3.x pyqt5 export-to-csv qfiledialog

当我尝试将我的表(QTableWidget)保存为csv文件时,所有内容都适合excel文件中的第一个单元格,我如何分隔每个单元格?这是我的保存功能:

def save_text(self, table):
    path = QFileDialog.getSaveFileName(self, 'Save CSV', os.getenv('HOME'), 'CSV(*.csv)')
    if path[0] != '':
        with open(path[0], 'w') as csv_file:
            writer = csv.writer(csv_file, dialect='excel')
            for row in range(table.rowCount()):
                row_data = []
                for column in range(table.columnCount()):
                    item = table.item(row, column)
                    if item is not None:
                        row_data.append(item.text())
                    else:
                        row_data.append('')
                writer.writerow(row_data)

1 个答案:

答案 0 :(得分:0)

尝试更换:

writer = csv.writer (csv_file, dialect = 'excel')

writer = csv.writer (csv_file, dialect = 'excel', delimiter = ';')