player-none 发表于 2026-3-4 13:57:18

Pickle 文件编辑器

本帖最后由 player-none 于 2026-3-4 14:02 编辑

原创作品;转载需标明“转载自 https://fishc.com.cn/thread-256250-1-1.html”

在无法直接使用 txt 文件储存数据时(即需要储存的对象不是字符串时),我们常用 pickle 模块将对象写入一个以二进制模式打开的文件中。

然而,有时程序会抽风,我们经常需要打开所写入的文件看看程序往文件里了些什么;有时候要加入一些用于调试的文本;也有时候是程序只有读取功能、没有写入功能,先弄点东西看看程序能不能正常运行。

这时,我们通常在文件夹内打开一个 Python 控制台临时写代码看文件内容;但由于人类是一种很懒的生物,每次都这样确实不太方便,于是我写了一个工具:

import pickle
from tkinter import *
from tkinter import filedialog as fd

def select():
    if path := fd.askopenfilename(title='打开文件…'):
      pathV.set(path)

def save():
    exec(importsT.get(1.0, END))
    with open(pathV.get(), 'wb') as f:
      pickle.dump(eval(textT.get(1.0, END)), f)

def read():
    with open(pathV.get(), 'rb') as f:
      textT.delete(1.0, END)
      textT.insert(1.0, repr(pickle.load(f)))

root = Tk()
root.title('Pickle 文件编辑器')
root.geometry('640x480')
root.resizable(False, False)

pathL = Label(root, text='请输入文件路径:')
pathL.place(x=10, y=10)
pathV = StringVar()
pathE = Entry(root, width=25, textvariable=pathV)
pathE.place(x=120, y=10)
selectB = Button(root, text=' 选择 ', command=select)
selectB.place(x=360, y=10)
readB = Button(root, text=' 打开 ', command=read)
readB.place(x=410, y=10)
saveB = Button(root, text=' 保存 ', command=save)
saveB.place(x=460, y=10)

textT = Text(root, width=40, height=20)
textT.place(x=10, y=50)
importsT = Text(root, width=25, height=5)
importsT.place(x=400, y=50)

for widget in (pathL, pathE, selectB, readB, saveB):
    widget.config(font=('Microsoft YaHei UI', 10, 'normal'))

for widget in (textT, importsT):
    widget.config(font=('Consolas', 13, 'normal'))

root.mainloop()


【本来想放个使用 GIF 的,但 GIF 太大了】

注意事项(必看):

[*]右边的大输入框用于应对使用了自定义对象(class xxx……xxx())的情况。若您使用了自定义对象,请记得在该输入框中导入,只需在该框内输入类似于 “from xxx import xxx” 的内容即可。
[*]若需要导入的模块不是 PIP 安装的模块(即您自己编写的 .py 文件),请使用以下代码模板:
import os
os.chdir('E:/xxx')
from xxx import xxx

[*]显示储存的内容采用了 repr() 函数,若您向内容中储存了自定义对象,请务必为其设置合理的 __repr__() 方法。

FishC 发表于 2026-3-4 13:58:38

这是一个使用 Tkinter 编写的 Pickle 二进制文件编辑器,允许用户通过图形界面读取和保存 Pickle 文件。以下是代码和详细说明。

import pickle
from tkinter import *
from tkinter import filedialog as fd

def select():
    if path := fd.askopenfilename(title='打开文件…'):
      pathV.set(path)

def save():
    exec(importsT.get(1.0, END))
    with open(pathV.get(), 'wb') as f:
      pickle.dump(eval(textT.get(1.0, END)), f)

def read():
    with open(pathV.get(), 'rb') as f:
      textT.delete(1.0, END)
      textT.insert(1.0, repr(pickle.load(f)))

root = Tk()
root.title('Pickle 文件编辑器')
root.geometry('640x480')
root.resizable(False, False)

pathL = Label(root, text='请输入文件路径:')
pathL.place(x=10, y=10)
pathV = StringVar()
pathE = Entry(root, width=25, textvariable=pathV)
pathE.place(x=120, y=10)
selectB = Button(root, text=' 选择 ', command=select)
selectB.place(x=360, y=10)
readB = Button(root, text=' 打开 ', command=read)
readB.place(x=410, y=10)
saveB = Button(root, text=' 保存 ', command=save)
saveB.place(x=460, y=10)

textT = Text(root, width=40, height=20)
textT.place(x=10, y=50)
importsT = Text(root, width=25, height=5)
importsT.place(x=400, y=50)

for widget in (pathL, pathE, selectB, readB, saveB):
    widget.config(font=('Microsoft YaHei UI', 10, 'normal'))

for widget in (textT, importsT):
    widget.config(font=('Consolas', 13, 'normal'))

root.mainloop()

功能概述:
此程序是一个简单的图形界面工具,用于编辑 Pickle 二进制文件。它允许用户选择文件、查看文件内容(以字符串形式显示)、编辑内容,并保存回 Pickle 格式。右侧文本框用于处理自定义对象的导入。

核心功能:

[*]选择文件:点击 选择 按钮浏览并选择 Pickle 文件。
[*]打开文件:点击 打开 按钮读取 Pickle 文件内容,并显示在左侧文本框中(使用 repr() 转换)。
[*]保存文件:点击 保存 按钮将左侧文本框中的内容保存为 Pickle 文件。程序会先执行右侧文本框中的导入语句,然后使用 eval() 将文本转换为 Python 对象。


重要注意事项:

[*]自定义对象导入:如果 Pickle 文件中包含自定义类或对象,必须在右侧文本框中导入相关模块。例如,输入 from mymodule import MyClass。
[*]本地模块导入:如果导入的模块是本地 .py 文件(非 PIP 安装),需先切换工作目录。参考模板:
import os
os.chdir('E:/xxx')
from xxx import xxx
[*]对象表示方法:建议为自定义类定义 __repr__() 方法,以便在文本框中正确显示内容。
[*]安全警告:使用 exec() 和 eval() 可能存在安全风险,请确保只编辑可信的文件内容。


简单示例:
假设有一个自定义类 Person 在文件 mymodule.py 中,且文件位于 E:/project。在右侧文本框输入:
import os
os.chdir('E:/project')
from mymodule import Person
然后,在左侧文本框中可以输入如 Person('Alice', 30) 的内容,保存后即可正确序列化。

希望这个解释能帮助你理解和使用该工具!如有疑问,欢迎继续交流。

以上回复来自 FishC AI 小助理,如未能正确解答您的问题,请继续追问。

sunshine_8205 发表于 2026-3-4 14:43:46

{:5_106:}
页: [1]
查看完整版本: Pickle 文件编辑器