How to read csv file(or any file) from GITLAB in python

Viewed 302

I have private repo in gitlab and I want to read a CSV file in python. How can I access gitlab from python? I tried this code but it is not working.

import requests
from io import StringIO
import pandas as pd

url = "https://gitlab.com/...../CSV_HOMEWORK.csv"
df = pd.read_csv(StringIO(requests.get(url).text))
print(df.head())

How do I need to pass my credentials?

Error I have enter image description here

1 Answers

See the Repository Files API access for GitLab -- that seems like what you're going for, since you're trying to access a file that is protected.

By creating an Access Token on GitLab for a user with access to the repository, you can pass this token alongside API requests for information.

If you wanted to use a library wrapper around the API rather than send/receive using GET/POST/etc, python-gitlab is a good library option.

Related