How to read .txt files from github into Google colab

放肆的年华 提交于 2020-12-30 04:27:52

问题


I have folder in github which contains text files and when I tried to read below code in Google colab I'm getting error

FileNotFoundError: [Errno 2] No such file or directory: 'https://github.com/Jainu-s/urldata/tree/master/al?raw=true'

loc = 'https://github.com/Jainu-s/urldata/tree/master/al?raw=true'
#uploaded = files.upload()
os.chdir(loc)
filelist = os.listdir()
#print (len((pd.concat([pd.read_csv(item, names=[item[:-4]]) for item in filelist],axis=1))))

data = []
path = loc
files = [f for f in os.listdir(path) if os.path.isfile(f)]
for f in files:
    with open(f,'r') as myfile:
        data.append(myfile.read())


df = pd.DataFrame(data,columns=['Data'])
print (df.shape)

回答1:


You can download all files in that directory to Colab first with:

!npx degit Jainu-s/urldata/al -f

Then, you can loop it like local files.




回答2:


import base64
import requests

master = "https://raw.githubusercontent.com/Jainu-s/urldata/master/al/abescoldbeer.com.txt"
req = requests.get(master)
req = req.text
print(req)

In this way you can read all the files using a for loop modifying the master string

https://stackoverflow.com/a/38497199/10077354 You can refer this link to know about reading github files.



来源:https://stackoverflow.com/questions/61723047/how-to-read-txt-files-from-github-into-google-colab

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!