【问题标题】:How do I get a file from github into my python code如何从 github 获取文件到我的 python 代码中
【发布时间】:2020-10-27 13:39:35
【问题描述】:

每次运行 python 脚本时,我都想读取位于存储库中的 csv 文件。我已经搜索了克隆 git 并找到并读取特定文件的方法,但没有成功。我看到了他们刚刚写的一个文件: !git clone ~the link of the repo~ 在代码的第一行没有导入任何东西。

我是一个绝对的初学者,很有可能我不会理解给我的方向。

【问题讨论】:

  • 您可能正在查看在 Jupyter 中运行的代码。 ! 允许运行 shell 命令。所以他们正在克隆 repo。
  • 也许你会在[这里]找到答案。我希望这有助于[这里]:stackoverflow.com/questions/14120502/…
  • 是的,代码在 Jupyter 中运行。但我也在 jupyter notebook 中编写我的代码,但它没有按预期运行

标签: python git


【解决方案1】:

假设存储库是公开的。

import requests

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

【讨论】:

    猜你喜欢
    • 2021-05-17
    • 1970-01-01
    • 2021-09-30
    • 1970-01-01
    • 2017-02-19
    • 1970-01-01
    • 1970-01-01
    • 2021-09-23
    • 1970-01-01
    相关资源
    最近更新 更多