Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/github/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
如何将python数据框架作为csv文件加载到Github存储库中?_Python_Github_Github Api - Fatal编程技术网

如何将python数据框架作为csv文件加载到Github存储库中?

如何将python数据框架作为csv文件加载到Github存储库中?,python,github,github-api,Python,Github,Github Api,我需要在服务器上部署Dash应用程序。对于数据存储库,我使用Github。所有被操纵的数据都需要存储在Github上,以便我的Dash应用程序能够访问它们 我遇到的所有解决方案都要求我在本地将数据帧保存为csv,然后将其提交给Github。在我的情况下,这是不可能的,我需要将数据帧作为csv直接提交到Github 提前感谢您的帮助。诀窍是将pandas数据框转换为文本,然后使用相同的格式上载文件。 这非常有帮助 我正在共享我当前使用的代码- #Import required packages i

我需要在服务器上部署Dash应用程序。对于数据存储库,我使用Github。所有被操纵的数据都需要存储在Github上,以便我的Dash应用程序能够访问它们

我遇到的所有解决方案都要求我在本地将数据帧保存为csv,然后将其提交给Github。在我的情况下,这是不可能的,我需要将数据帧作为csv直接提交到Github


提前感谢您的帮助。

诀窍是将pandas数据框转换为文本,然后使用相同的格式上载文件。 这非常有帮助

我正在共享我当前使用的代码-

#Import required packages
import pandas as pd
from github import Github
from github import InputGitTreeElement
from datetime import datetime

#create test pd df to upload
d = {'col1': [1, 2, 3], 'col2': [4, 5, 6]}
df = pd.DataFrame(d)
#convert pd.df to text. This avoids writing the file as csv to local and again reading it
df2 = df.to_csv(sep=',', index=False)

#list files to upload and desired file names with which you want to save on GitHub
file_list = [df2,df2]
file_names = ['Test.csv','Test2.csv']

#Specify commit message
commit_message = 'Test Python'

#Create connection with GiHub
user = "{your-user-id}"
password = "{your-password}"
g = Github(user,password)

#Get list of repos
for repo in g.get_user().get_repos():
    print(repo.name)
    repo.edit(has_wiki=False)

#Create connection with desired repo
repo = g.get_user().get_repo('{your-repo-name}')

#Check files under the selected repo
x = repo.get_contents("")
for labels in x:
    print(labels)
x = repo.get_contents("Test.csv") #read a specific file from your repo

#Get available branches in your repo
x = repo.get_git_refs()
for y in x:
    print(y)
# output eg:- GitRef(ref="refs/heads/master")

#Select required branch where you want to upload your file.
master_ref = repo.get_git_ref("heads/master")

#Finally, putting everything in a function to make it re-usable

def updategitfiles(file_names,file_list,userid,pwd,Repo,branch,commit_message =""):
    if commit_message == "":
       commit_message = "Data Updated - "+ datetime.now().strftime('%Y-%m-%d %H:%M:%S')

    g = Github(userid,pwd)
    repo = g.get_user().get_repo(Repo)
    master_ref = repo.get_git_ref("heads/"+branch)
    master_sha = master_ref.object.sha
    base_tree = repo.get_git_tree(master_sha)
    element_list = list()
    for i in range(0,len(file_list)):
        element = InputGitTreeElement(file_names[i], '100644', 'blob', file_list[i])
        element_list.append(element)
    tree = repo.create_git_tree(element_list, base_tree)
    parent = repo.get_git_commit(master_sha)
    commit = repo.create_git_commit(commit_message, tree, [parent])
    master_ref.edit(commit.sha)
    print('Update complete')

updategitfiles(file_names,file_list,user,password,'{your-repo-name}','{your-branch-name}')