保存的文件中缺少列 headers
Missing column headers in saved file
我可以在这里得到帮助吗?
我的函数在每一行之后保存一个带有 space 的文件,并且不保存列 headers。
这是函数。
def download_results(self):
try:
path = QFileDialog.getSaveFileName(MainWindow, 'Save results', os.getenv('HOME'), 'CSV(*.csv)')
if path[0] != '':
with open(path[0], 'w') as csv_file:
writer = csv.writer (csv_file, dialect = 'excel', delimiter = ',')
for row in range(self.analysis_table.rowCount()):
row_data = []
for column in range(self.analysis_table.columnCount()):
item = self.analysis_table.item(row, column)
if item is not None:
row_data.append(item.text())
else:
row_data.append('')
writer.writerow(row_data)
QMessageBox.information(MainWindow,"Success","Succeeded")
except:
QMessageBox.warning(MainWindow,"Failed","Operation failed.")
您必须使用 horizontalHeaderItem()
方法从 QTableWidgetItem 中提取文本:
def download_results(self):
try:
path, _ = QFileDialog.getSaveFileName(
MainWindow, "Save results", os.getenv("HOME"), "CSV(*.csv)"
)
if path:
with open(path, "w", <b>newline=''</b>) as csv_file:
writer = csv.writer(csv_file)
<b>headers = []
for c in range(self.analysis_table.columnCount()):
it = self.analysis_table.horizontalHeaderItem(c)
if it is not None:
headers.append(it.text())
else:
headers.append("")
writer.writerow(headers)</b>
for row in range(self.analysis_table.rowCount()):
row_data = []
for column in range(self.analysis_table.columnCount()):
item = self.analysis_table.item(row, column)
if item is not None:
row_data.append(item.text())
else:
row_data.append("")
writer.writerow(row_data)
QMessageBox.information(MainWindow, "Success", "Succeeded")
except:
QMessageBox.warning(MainWindow, "Failed", "Operation failed.")
做到了。感谢贡献者
def download_results(self):
try:
path = QFileDialog.getSaveFileName(MainWindow, "Save results", os.getenv("HOME"), "CSV(*.csv)")
if path != "":
with open(path[0], "w", newline='') as csv_file:
writer = csv.writer(csv_file)
headers = []
for c in range(self.analysis_table.columnCount()):
it = self.analysis_table.horizontalHeaderItem(c)
if it is not None:
headers.append(it.text())
else:
headers.append("")
writer.writerow(headers)
for row in range(self.analysis_table.rowCount()):
row_data = []
for column in range(self.analysis_table.columnCount()):
item = self.analysis_table.item(row, column)
if item is not None:
row_data.append(item.text())
else:
row_data.append("")
writer.writerow(row_data)
QMessageBox.information(MainWindow, "Success", "Succeeded")
except:
QMessageBox.warning(MainWindow, "Failed", "Operation failed.")
我可以在这里得到帮助吗? 我的函数在每一行之后保存一个带有 space 的文件,并且不保存列 headers。 这是函数。
def download_results(self):
try:
path = QFileDialog.getSaveFileName(MainWindow, 'Save results', os.getenv('HOME'), 'CSV(*.csv)')
if path[0] != '':
with open(path[0], 'w') as csv_file:
writer = csv.writer (csv_file, dialect = 'excel', delimiter = ',')
for row in range(self.analysis_table.rowCount()):
row_data = []
for column in range(self.analysis_table.columnCount()):
item = self.analysis_table.item(row, column)
if item is not None:
row_data.append(item.text())
else:
row_data.append('')
writer.writerow(row_data)
QMessageBox.information(MainWindow,"Success","Succeeded")
except:
QMessageBox.warning(MainWindow,"Failed","Operation failed.")
您必须使用 horizontalHeaderItem()
方法从 QTableWidgetItem 中提取文本:
def download_results(self):
try:
path, _ = QFileDialog.getSaveFileName(
MainWindow, "Save results", os.getenv("HOME"), "CSV(*.csv)"
)
if path:
with open(path, "w", <b>newline=''</b>) as csv_file:
writer = csv.writer(csv_file)
<b>headers = []
for c in range(self.analysis_table.columnCount()):
it = self.analysis_table.horizontalHeaderItem(c)
if it is not None:
headers.append(it.text())
else:
headers.append("")
writer.writerow(headers)</b>
for row in range(self.analysis_table.rowCount()):
row_data = []
for column in range(self.analysis_table.columnCount()):
item = self.analysis_table.item(row, column)
if item is not None:
row_data.append(item.text())
else:
row_data.append("")
writer.writerow(row_data)
QMessageBox.information(MainWindow, "Success", "Succeeded")
except:
QMessageBox.warning(MainWindow, "Failed", "Operation failed.")
做到了。感谢贡献者
def download_results(self):
try:
path = QFileDialog.getSaveFileName(MainWindow, "Save results", os.getenv("HOME"), "CSV(*.csv)")
if path != "":
with open(path[0], "w", newline='') as csv_file:
writer = csv.writer(csv_file)
headers = []
for c in range(self.analysis_table.columnCount()):
it = self.analysis_table.horizontalHeaderItem(c)
if it is not None:
headers.append(it.text())
else:
headers.append("")
writer.writerow(headers)
for row in range(self.analysis_table.rowCount()):
row_data = []
for column in range(self.analysis_table.columnCount()):
item = self.analysis_table.item(row, column)
if item is not None:
row_data.append(item.text())
else:
row_data.append("")
writer.writerow(row_data)
QMessageBox.information(MainWindow, "Success", "Succeeded")
except:
QMessageBox.warning(MainWindow, "Failed", "Operation failed.")