I am trying to split github.com/pmezard/go-difflib v1.0.0 into name and version
I tried like this but it only split into 2. output i am getting is [{‘name’: ‘pmezard’, ‘version’: ‘v1.0.0n’}]
Expected output [{‘name’: ‘go-difflib’, ‘version’: ‘v1.0.0’}]
path = foldername + "/example.txt" print(path) file = open(path) Lines = file.readlines() List = [] for line in Lines: depends = {} if (len(line.split(" ")) > 1): depends["name"] = line.split(" ")[1] depends["version"] = line.split(" ")[1] List.append(depends)
Advertisement
Answer
Considering all string of format github.com/pmezard/go-difflib v1.0.0
path = foldername + "/example.txt" print(path) file = open(path) Lines = file.readlines() file.close() # close file List = [] for line in Lines: try: link, version = line.split(" ") except ValueError: continue name = link.split("/")[-1] List.append({"name": name, "version": version})
For complex pattern, regex may be helpful