2021年1月25日星期一

python reverse solve a formatted string

I am trying to do opposite of format function to get a dictionary of key value pair, what I have is a path template with keys defined and I have final path

path_template = "/server/{project}/{task}/myfile.{extension}"    mypath_a = "/server/test_project/my_task/myfile.txt"  mypath_b = "/server/test_project/my_task_j/myfile.json"  

I want to be able to parse the path and build a dictionary like below, anyone has any suggestion on the best way to approach this or python string formaing may already have some functionality to parse this

parse_path(mypath_a)    {"project":"test_project", "task":"my_task", "extension":"txt"}  
https://stackoverflow.com/questions/65894867/python-reverse-solve-a-formatted-string January 26, 2021 at 09:49AM

没有评论:

发表评论