遍历旧历史文件的行并通过HistoryManager
对象向新历史写入新会话是相当简单的:
from IPython.core.interactiveshell import InteractiveShell
from IPython.core.history import HistoryManager
def migrate_history(old_histfile, new_histfile):
"""populate an IPython sqlite history from an old readline history file"""
# shell = InteractiveShell.instance()
history = HistoryManager(hist_file=new_histfile,
# this line shouldn't be necessary, but it is in 0.13
shell = InteractiveShell.instance()
)
with open(old_histfile) as f:
# iterate through readline history,
# and write to the new history database
for linenum, line in enumerate(f):
history.store_inputs(linenum, line)
此函数作为脚本,您可以运行一次以将旧的 IPython 历史记录到新的历史记录中。