|
楼主 |
发表于 2024-11-4 10:43:33
|
显示全部楼层
Exception in Tkinter callback
Traceback (most recent call last):
File "D:\PYTHON_NEW\pack20240926\pythonProject1\.venv\Lib\site-packages\pandas\core\internals\construction.py", line 939, in _finalize_columns_and_data
columns = _validate_or_indexify_columns(contents, columns)
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "D:\PYTHON_NEW\pack20240926\pythonProject1\.venv\Lib\site-packages\pandas\core\internals\construction.py", line 986, in _validate_or_indexify_columns
raise AssertionError(
AssertionError: 5 columns passed, passed data had 6 columns
The above exception was the direct cause of the following exception:
Traceback (most recent call last):
File "C:\Users\Administrator\AppData\Local\Programs\Python\Python312\Lib\tkinter\__init__.py", line 1967, in __call__
return self.func(*args)
^^^^^^^^^^^^^^^^
File "D:\PYTHON_NEW\pack20240926\pythonProject1\cadworx_excel.py", line 69, in run
df = pd.DataFrame(data, columns=columns)
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "D:\PYTHON_NEW\pack20240926\pythonProject1\.venv\Lib\site-packages\pandas\core\frame.py", line 851, in __init__
arrays, columns, index = nested_data_to_arrays(
^^^^^^^^^^^^^^^^^^^^^^
File "D:\PYTHON_NEW\pack20240926\pythonProject1\.venv\Lib\site-packages\pandas\core\internals\construction.py", line 520, in nested_data_to_arrays
arrays, columns = to_arrays(data, columns, dtype=dtype)
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "D:\PYTHON_NEW\pack20240926\pythonProject1\.venv\Lib\site-packages\pandas\core\internals\construction.py", line 845, in to_arrays
content, columns = _finalize_columns_and_data(arr, columns, dtype)
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "D:\PYTHON_NEW\pack20240926\pythonProject1\.venv\Lib\site-packages\pandas\core\internals\construction.py", line 942, in _finalize_columns_and_data
raise ValueError(err) from err
ValueError: 5 columns passed, passed data had 6 columns
上面是修改后的报错"""
首先,我们需要使用Python的tkinter库来创建一个简单的图形界面,让用户选择txt文件和输出文件夹。然后,使用pandas库将txt文件内容读取到DataFrame中,并将数据写入Excel文件。
解析:
1. 导入所需库:tkinter、pandas、openpyxl
2. 创建图形界面,让用户选择txt文件和输出文件夹
3. 读取txt文件内容,将其转换为DataFrame
4. 将DataFrame写入Excel文件
代码如下:
```python
"""
import pandas as pd
import tkinter as tk
from tkinter import filedialog
from tkinter import ttk
import time
from openpyxl import load_workbook
import chardet
input_file_path = ""
tem_file_path = ""
tar_folder_path = ""
df_merge = []
def select_input_file():
global input_file_path
input_file_path = filedialog.askopenfilename(filetypes=[("text files", "*.txt")], title="请选择txt,.txt格式")
if input_file_path:
input_file.set(input_file_path)
input_file_label.config(text=input_file_path.split('/')[-1])
def select_target_folder():
global tar_folder_path
tar_folder_path = filedialog.askdirectory(title="请选择输出文件夹")
if tar_folder_path:
target_folder.set(tar_folder_path)
target_folder_label.config(text=tar_folder_path.split('/')[-1])
def run():
global input_file_path
global tem_file_path
global tar_folder_path
global df_merge
status_label.config(text="正在运行中,请稍候")
root.update()
time.sleep(2) # 模拟命令执行时间
status_label.config(text="已完成")
input_file = input_file_path
template_file = tem_file_path
target_folder = tar_folder_path
# 读取txt文件内容,转换为DataFrame
with open(input_file, 'rb') as f:
raw_data = f.read()
result = chardet.detect(raw_data)
encoding = result['encoding']
with open(input_file, 'r', encoding= encoding, errors='ignore') as f:
lines = f.readlines()
data = [line.strip().split() for line in lines]
columns = ['管线号', '焊缝编号', '焊接类型', '主要信息', '管线寸口']
df = pd.DataFrame(data, columns=columns)
# 对列“焊缝编号”的第一个字符去除
df['焊缝编号'] = df['焊缝编号'].str[1:]
# 对列“焊接类型”的数据进行替换
df['焊接类型'] = df['焊接类型'].replace(
{'BW': '对焊', 'LET': '开口焊', 'SW': '承插焊', 'SOF': '承插焊', 'SOB': '承插焊'})
df['主管外径'] = df['主要信息'].apply(lambda x: x[1:x.find('*')])
df['支管外径'] = df['主管外径']
df['壁厚'] = df['主要信息'].apply(lambda x: x[x.find('*') + 1:x.find(',')])
df['支管壁厚'] = df['壁厚']
df['焊缝前材质'] = df['主要信息'].apply(lambda x: x[x.find(',') + 1:]) + "-"
df['焊缝后材质'] = df['焊缝前材质']
df['弯头焊点标识'] = "-"
# df['管线寸口'] = ""
# 将DataFrame写入Excel文件
excel_file = f"{target_folder}/转换后excel.xlsx"
df.to_excel(excel_file, index=False)
root = tk.Tk()
root.title("江苏瑞鼎 CADWORK的txt文件转excel程序")
root.geometry("600x500+100+100")
# 设置窗口锁定缩放,可以传两个bool值,指定能否缩放,True是可以拉伸的。
root.resizable(False, False)
# 设置窗口置顶,用的到,可以用
root.attributes("-topmost", True)
input_file = tk.StringVar()
template_file = tk.StringVar()
target_folder = tk.StringVar()
# 创建按钮组件 Button,注意这里都是写函数名,不加()
tk.Button(root, text="1、选择txt格式,格式为.txt", width=32, command=select_input_file, font=("黑体", 20)).place(x=20, y=20)
input_file_label = tk.Label(root, text="",width=40,font=("楷体", 20))
input_file_label.place(x=20, y=70)
tk.Button(root, text="2、选择输出位置,格式为文件夹", width=35, command=select_target_folder, font=("黑体", 20)).place(
x=20, y=120)
target_folder_label = tk.Label(root, text="",width=40,font=("楷体", 20))
target_folder_label.place(x=20, y=170)
# 填写一个运行的按钮
tk.Button(root, text="3、前面完成输入,按此运行", width=30, command=run, font=("黑体", 20)).place(
x=20, y=220)
status_label = tk.Label(root, text="等待中",width=30,font=("楷体", 20))
status_label.place(x=20, y=270)
version_label = tk.Label(root, text="软件版本:V1.0 20241031",width=30,font=("宋体", 20)).place(x=20, y=350)
author_label = tk.Label(root, text="制作:朱宁",width=30,font=("宋体", 20)).place(x=20, y=400)
# 开启窗口对象
root.mainloop()
|
|