Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
menu search
person
Welcome To Ask or Share your Answers For Others

Categories

初学python,最近尝试爬数据,json字符串的value中有冒号,需要去掉。我的代码如下。
a和b都是value中会有冒号的字符串

import re
a = "Title:'Intern: Customer Experience + Innovation (CX+I) Intern Brands'"
b = "cmp:'Adecco: USA',cmpesc:'Adecco: USA'"
result = re.sub('^(?:Title|cmp|cmpesc):.+(:)','', a)

代码执行结果是只剩 Customer Experience + Innovation (CX+I) Intern Brands',之前的内容全被删除了,而我想要的效果是只删intern之后的那个冒号(title后的冒号要保留)。
请问大家该如何修改?


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
449 views
Welcome To Ask or Share your Answers For Others

1 Answer

import re
result = re.sub('^(Title|cmp|cmpesc:)(.+):(.*)',
                '\1\2\3',
                "Title:'Intern: Customer Experience + Innovation (CX+I) Intern Brands'")

print(result) # Title:'Intern Customer Experience + Innovation (CX+I) Intern Brands'

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
...