【发布时间】:2019-11-26 23:08:09
【问题描述】:
我想提取${ 和} 之间的字符串。
例子:
/one/${two}
/${three}/one/${four}
/five/${six}/seven
我有代码,它从 json 字符串中读取内容,除了 API 端点之外什么都没有。在此过程中,我想提取${ 和} 之间的单词或字符串并将它们存储在一个变量中。
下面是从 json 文件中读取数据内容的相同代码,
with open("example.json", "r") as reading:
data = json.load(reading)
for path, values in data['paths'].items():
print(path.replace('{', '${')) # If required used print statement
for value in values:
print(value)
输出:
two
three
four
six
【问题讨论】:
-
为什么不使用正则表达式捕获组?
标签: python python-3.x string slice