4

I'm looking to pull a specific file from a github repo and load into a dictionary in python and then write the file back to a specific folder in the github repo.

Suppose, my folder structure is:

Github Repo > files > rawfiles > test.txt

Github Repo > files > output > output.txt

How would I extract one file from Github and then write it back to Github? Below is what I have created, but the output seems to be including all the HTML tags.

import csv
import urllib
url = 'GithubRepo/rawfiles/test.txt'
webpage = urllib.urlopen(url)
data = []
datareader = csv.reader(webpage)
    for row in datareader:
    data.append(row)

print data

3
  • Do you know how Git works (distributed model, commits and commit messages, remotes, etc.)? Commented Jul 15, 2018 at 20:06
  • If you are shure that you want to use Git(hub) you can use the API developer.github.com/v3/git or git itself. Commented Jul 15, 2018 at 20:19
  • did you try to urlopen this link instead? https://raw.githubusercontent.com/your_repo.. Commented Jul 15, 2018 at 20:26

1 Answer 1

1

you should try to .read() the url open request.

lets say you want to parse https://github.com/codeforamerica/ohana-api/blob/master/data/sample-csv/addresses.csv

so you change the appendinx to https://raw.githubusercontent.com

and writing the following code

import urllib.request as request
import csv
r = request.urlopen('https://raw.githubusercontent.com/codeforamerica/ohana-api/master/data/sample-csv/addresses.csv').read().decode('utf8').split("\n")
reader = csv.reader(r)
for line in reader:
    print(line)
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.