我有一个本地 git 存储库,我正在尝试找到一种方法将 xlsx 文件的特定版本放入我的 Python 代码中,以便我可以使用 pandas 处理它。
我找到了 gitpython lib;但我不知道如何正确使用它。
repo = Repo(path_to_repo)
commit = repo.commit(sha)
targetfile = commit.tree / 'dataset.xlsx'
我不知道下一步该做什么。 我尝试使用路径将其加载到pandas;但是,当然,它只是加载我的最新版本。
如何将以前版本的xlsx加载到pandas?
最佳答案
当您请求 commit.tree/'dataset.xlsx'
时,您会得到一个 git.Blob
对象:
>>> targetfile
<git.Blob "3137d9443f54325b8ad8a263b13053fee47fbff2">
如果你想读取对象的内容,可以使用data_stream
方法提取内容,该方法返回一个类似文件的对象:
>>> data = targetfile.data_stream.read()
或者您可以使用 stream_data
方法(别看我,我没有命名它们),它将数据写入类似文件的对象:
>>> import io
>>> buf = io.BytesIO()
>>> targetfile.stream_data(buf)
<git.Blob "3137d9443f54325b8ad8a263b13053fee47fbff2">
>>> buf.getvalue()
b'The contents of the file...'
关于python - 如何使用 python 从 git 存储库获取特定文件版本,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/71869347/