Python 如何读取Github存储库中的所有文本文件?
我想读取Github存储库中的所有文本文件,但文本文件地址与原始文本地址不同。 例如,请查看此链接: 现在,speech_00.txt在原始模式下与具有不同的地址 如果不编辑地址(例如添加 githubusercontent或删除blob) 此外,我还使用以下代码读取了一个示例文本文件:Python 如何读取Github存储库中的所有文本文件?,python,github,repository,Python,Github,Repository,我想读取Github存储库中的所有文本文件,但文本文件地址与原始文本地址不同。 例如,请查看此链接: 现在,speech_00.txt在原始模式下与具有不同的地址 如果不编辑地址(例如添加 githubusercontent或删除blob) 此外,我还使用以下代码读取了一个示例文本文件: import urllib response = urllib.request.urlopen("https://raw.githubusercontent.com/PedramNavid/trump_s
import urllib
response = urllib.request.urlopen("https://raw.githubusercontent.com/PedramNavid/trump_speeches/master/data/speech_72.txt")
Text = response.read()
Text=Text.decode("utf-8")
实现这一点的一种简单方法(基于该目录的特定方式) 结构化)将循环地添加到您输入的字符串中 作为您的文件路径:
import urllib
# Get master directory
speech_dir ="https://raw.githubusercontent.com/PedramNavid/trump_speeches/master/data/"
# Iterate through all speeches in directory, from 00 to 73
cur_speech = 00
end_speech = 73
while (cur_speech <= end_speech):
# Change the speech you want to get
speech_nm = ('speech_' + str(cur_speech) +'.txt')
response = urllib.request.urlopen(speech_nm)
# Do what you need to with the speech
Text = response.read()
Text = Text.decode("utf-8")
# Update to the new speech
cur_speech +=1
导入urllib
#获取主目录
语音_dir=”https://raw.githubusercontent.com/PedramNavid/trump_speeches/master/data/"
#遍历目录中从00到73的所有演讲
cur_speech=00
结束语=73
虽然(cur_speech我使用您的代码(@N.Yasarturk),我编辑它以获取所有文件。但我问,是否有其他方法(不编辑地址)从Github存储库读取这些文件
import urllib
# Get master directory
speech_dir ="https://raw.githubusercontent.com/PedramNavid/trump_speeches/master/data/"
# Iterate through all speeches in directory, from 00 to 73
cur_speech = 0
temp=str(cur_speech)
end_speech = 73
while (cur_speech <= end_speech):
# Change the speech you want to get
if(cur_speech<10):
temp="0"+str(cur_speech)
else:
temp=str(cur_speech)
speech_nm = (speech_dir+'speech_' + temp +'.txt')
print(speech_nm)
response = urllib.request.urlopen(speech_nm)
# Do what you need to with the speech
Text = response.read()
Text = Text.decode("utf-8")
print(Text)
# Update to the new speech
cur_speech +=1
导入urllib
#获取主目录
语音_dir=”https://raw.githubusercontent.com/PedramNavid/trump_speeches/master/data/"
#遍历目录中从00到73的所有演讲
cur_语音=0
temp=str(当前语音)
结束语=73
while(cur_)speech我在Colab中测试了你们的解决方案,但它有一些错误。若我想使用这个方法,我可以通过处理每个文件名的字符串来编写它,但并没有任何错误。但你们使用的是字符串修改方法。