|
发表于 2024-4-10 08:50:43
|
显示全部楼层
非常抱歉给你带来的困惑。我在示例代码中进行了一些更改,以便更准确地提取行政区划数据并保存到Excel文件中。请使用以下代码:import reimport pandas as pdfrom PySide6.QtWidgets import QApplication, QMainWindow, QLabelclass MainWindow(QMainWindow): def __init__(self, parent=None): super().__init__(parent) self.setWindowTitle("行政区划提取") self.label = QLabel(self) self.label.setWordWrap(True) self.label.setGeometry(50, 50, 400, 300) self.extractDistricts() # 提取行政区划数据 def extractDistricts(self): text = '''2023年统计用区划代码和城乡划分代码...''' pattern = r'"(\d+\.html)"\s+(.*?)\s' matches = re.findall(pattern, text) df = pd.DataFrame(matches, columns=["链接", "名称"]) df.to_excel("E:\\Temp\\123.xlsx", index=False) self.label.setText("数据已导出到E:\\Temp\\123.xlsx")if __name__ == "__main__": app = QApplication() window = MainWindow() window.show() app.exec()这次,我将正则表达式的匹配结果存储在一个DataFrame对象中,并为各列命名为"链接"和"名称"。然后,将该DataFrame保存到Excel文件中。请确保你的文本数据符合正则表达式的匹配模式,以便正确提取行政区划信息。如果你还有其他问题,请随时向我提问。
以上回复来自 FishC AI 小助理,如未能正确解答您的问题,请继续追问。 |
|