如何从 github 获取文件到我的 python 代码中

问题描述 投票:0回答:2

每次运行 python 脚本时,我想读取位于存储库中的 csv 文件。我已经搜索了克隆 git 并查找和读取特定文件的方法,但没有成功。我看到一个文件,他们刚刚写道:

!git clone ~the link of the repo~
在代码的第一行而不导入任何内容。

我是一个绝对的初学者,我很可能不明白给我的指示。

python git
2个回答
0
投票

假设存储库是公共的。

import requests

url = "path/to/raw/file.csv"
resp = requests.get(url)
with open("myfile.csv", "w") as f:
    f.write(resp.text)

0
投票

你能解释一下代码吗,我的意思是像 doe “f” 做什么?

© www.soinside.com 2019 - 2024. All rights reserved.