我需要從 keys 中刪除 string 中的引號(hào)。這是一個(gè)代碼:import remystr = '["key": val, "key2": val, "key3": v, "key5": 34, "key6": "ef"]'key_pattern = "(\"(?P<key>\w+)\": )"pattern = re.compile(key_pattern)print(re.sub(pattern, "\key", mystr))這是行不通的。我需要得到結(jié)果:mystr = '[key: val, key2: val, key3: v, key5: 34, key6: "ef"]'
1 回答

猛跑小豬
TA貢獻(xiàn)1858條經(jīng)驗(yàn) 獲得超8個(gè)贊
您可以使用
"(\w+)"(?=:)
請(qǐng)參閱正則表達(dá)式演示。細(xì)節(jié):
"
- 一個(gè)"
字符(\w+)
- 第 1 組:一個(gè)或多個(gè)字母、數(shù)字、_
"
- 一個(gè)"
字符(?=:)
- 下一個(gè)字符必須是:
請(qǐng)參閱Python 演示:
import re
mystr = '["key": val, "key2": val, "key3": v, "key5": 34, "key6": "ef"]'
key_pattern = '"(\w+)"(?=:)'
print(re.sub(key_pattern, r"\1", mystr))
# => [key: val, key2: val, key3: v, key5: 34, key6: "ef"]
添加回答
舉報(bào)
0/150
提交
取消