如何使用panda或请求访问Python中的私有Github Repo文件(.csv)

如何使用panda或请求访问Python中的私有Github Repo文件(.csv),python,pandas,git,csv,private,Python,Pandas,Git,Csv,Private,我必须将我的公共Github存储库切换到私有,并且无法访问文件,而不能使用公共Github repo能够访问的访问令牌 我可以使用curl访问私人回购的CSV: ''' curl-s https://{token}@raw.githubusercontent.com/username/repo/master/file.csv ''' 但是,我希望在python文件中访问此信息。当回购协议公开时,我可以简单地使用: ''' url=“” df=pd.read\u csv(url,错误线=False

我必须将我的公共Github存储库切换到私有,并且无法访问文件,而不能使用公共Github repo能够访问的访问令牌

我可以使用curl访问私人回购的CSV: ''' curl-s https://{token}@raw.githubusercontent.com/username/repo/master/file.csv

'''

但是,我希望在python文件中访问此信息。当回购协议公开时,我可以简单地使用: ''' url=“” df=pd.read\u csv(url,错误线=False)

'''

由于回购协议是私有的,所以这种方法不再有效,我也找不到替代从终端下载的方法来下载python中的CSV

如果我尝试: ''' requests.get(https://{token}@raw.githubusercontent.com/username/repo/master/file.csv) ''' 我得到一个404响应,这与pd.read\u csv()基本相同。如果单击原始文件,我会看到创建了一个临时令牌,URL为: ''' '''
有没有一种方法可以附加我的永久私有访问令牌,这样我就可以始终从github中提取此数据?

您看过
pygithub
了吗?对于访问回购协议、文件、拉取请求、历史记录等非常有用。文档是。下面是一个示例脚本,它打开一个拉取请求、一个基本分支的新分支(您需要该访问令牌,或者生成一个新令牌!),并删除一个文件:

from github import Github
my_reviewers = ['usernames', 'of_reviewers']
gh = Github("<token string>")
repo_name = '<my_org>/<my_repo>'
repo = gh.get_repo(repo_name)
default_branch_name = repo.default_branch
base = repo.get_branch(default_branch_name)
new_branch_name = "my_new_branchname"
new_branch = repo.create_git_ref(ref=f'refs/heads/{new_branch_name}',sha=base.commit.sha)
contents = repo.get_contents("some_script_in_repo.sh", ref=new_branch_name)
repo.delete_file(contents.path, "commit message", contents.sha, branch=new_branch_name)
pr = repo.create_pull(
    title="PR to Remove some_script_in_repo.sh",
    body="This is the text in the main body of your pull request",
    head=new_branch_name,
    base=default_branch_name,
)
pr.create_review_request(reviewers=my_reviewers)
从github导入github
my_reviewers=['usernames','of_reviewers']
gh=Github(“”)
回购名称='/'
repo=gh.get\u repo(repo\u名称)
默认分支机构名称=回购。默认分支机构
base=repo.get\u分支(默认分支名称)
新分支机构名称=“我的新分支机构名称”
new_branch=repo.create_git_ref(ref=f'refs/heads/{new_branch_name}',sha=base.commit.sha)
contents=repo.get\u contents(“repo.sh中的某些脚本”,ref=new\u分支机构名称)
repo.delete_文件(contents.path,“commit message”,contents.sha,branch=new_branch_name)
pr=回购。创建(
title=“PR删除\u repo.sh中的一些\u脚本”,
body=“这是拉取请求主体中的文本”,
head=新分支机构名称,
base=默认分支机构名称,
)
pr.create\u review\u请求(审阅者=我的审阅者)

希望对你有帮助,编码快乐

是的,您可以下载Python中的CSV文件,而不是从终端下载。为了实现这一点,您可以使用GitHub API v3,并提供“请求”和“io”模块协助。可复制的例子如下

将numpy导入为np
作为pd进口熊猫
导入请求
从io导入StringIO
#创建CSV文件
df=pd.DataFrame(np.random.randint(2,大小=10_000)。重塑(1_000,10))
df.to_csv('filename.csv')
#->现在将文件上载到私有github repo
#定义请求的参数
令牌='粘贴您的个人访问令牌'
所有者='存储库所有者名称'
repo='存储数据的存储库名称'
路径='filename.csv'
#发送请求
r=requests.get(
'https://api.github.com/repos/{owner}/{repo}/contents/{path}.格式(
所有者=所有者,回购=回购,路径=路径),
标题={
“接受”:“application/vnd.github.v3.raw”,
“授权”:“令牌{}”。格式(令牌)
}
)
#将字符串转换为StringIO对象
string_io_obj=StringIO(r.text)
#将数据加载到df
df=pd.read\u csv(字符串,sep=“,”,index\u col=0)
#可选地将df写入CSV
df.to_csv(“文件名_02.csv”)

这就是我的最终目标——如果有人遇到同样的问题,就把它留在这里。谢谢你的帮助

    import json, requests, urllib, io

    user='my_github_username'
    pao='my_pao'

    github_session = requests.Session()
    github_session.auth = (user, pao)

    # providing raw url to download csv from github
    csv_url = 'https://raw.githubusercontent.com/user/repo/master/csv_name.csv'

    download = github_session.get(url_swing).content
    downloaded_csv = pandas.read_csv(io.StringIO(download.decode('utf-8')), error_bad_lines=False)

这种方式对我来说非常有效:

    def _github(url: str, mode: str = "private"):
        url = url.replace("/blob/", "/")
        url = url.replace("/raw/", "/")
        url = url.replace("github.com/", "raw.githubusercontent.com/")

        if mode == "public":
            return requests.get(url)
        else:
            token = os.getenv('GITHUB_TOKEN', '...')
            headers = {
                'Authorization': f'token {token}',
                'Accept': 'application/vnd.github.v3.raw'}
            return requests.get(url, headers=headers)

嘿,我正在处理一个类似的私有repo(.csv)文件,我想知道除了读取文件外,我还可以通过什么方式编辑github上托管的文件。直觉上,我认为我应该能够编辑数据,因为我可以读取数据,但我无法找到解决方案。