如何在不使用任何库的情况下在python中解析文本文件并读取包含git repos URL的行?
注意:该文本文件包含10个git存储库的URL。
文本文件格式:
repo_name https://www.github.com/path/to/repo
repo_name https://www.github.com/path/to/repo
repo_name https://www.github.com/path/to/repo
编辑:对其进行了排序。
def main():
# reading txt file
opFile = open("testrepo/textfile.txt", "r")
lines = opFile.readlines()
opFile.close()
for line in lines:
line = line.strip()
parts = line.split()
repo_name = parts[0]
branch = parts[1]
git_url = parts[2]
print("Repository name: {} \n Branch name: {} \n Git clone url: {}".format(repo_name, branch, git_url))
if __name__ == "__main__":
main()
输出:
Repository name: repo_name
Branch name: master
Git clone url: https://gitlab.com/path/to/repo.git
答案 0 :(得分:0)
您的文件只是两部分:
repo_name ==> [0]
https://www.github.com/path/to/repo ==> [1]
所以您将给出一个错误:
IndexError: list index out of range