英文:
Find the all the occurrence of pattern in string and then replace in python
问题
我想用一个新字符串替换所有包含location_ids的方括号(已经高亮显示的部分)。是否可以做到?我考虑使用正则表达式,但我没有找到任何通用模式。
英文:
I have a very big string from which I wanted to replace a specific substring with another string. Example of sample string:
txt = """{u'lang': u'en_US', u'tz': u'Canada/Eastern', u'uid': 1, u'dashboard_merge_domains_contexts': False, u'group_by': [], u'params': {u'action': 315}}" domain="[[u'state', u'!=', u'done'], [u'state', u'!=', u'cancel'], [u'state', u'!=', u'draft'], **[u'location_ids', u'ilike', u'WH07']**, [u'state', u'!=', u'done'], [u'state', u'!=', u'cancel'], [u'state', u'!=', u'draft'], **[u'location_ids', u'=', u'WH123']**]"""
I want to replace all square brackets containing location_ids(highlighted) with a new string. is it possible to do? I thought of using regex but I did not find any common pattern.
答案1
得分: 1
以下是将字符串转换回Python列表的代码以及解析列表的代码部分:
import ast
txt = "{u'lang': u'en_US', u'tz': u'Canada/Eastern', u'uid': 1, u'dashboard_merge_domains_contexts': False, u'group_by': [], u'params': {u'action': 315}}"
domain = "[[u'state', u'!=', u'done'], [u'state', u'!=', u'cancel'], [u'state', u'!=', u'draft'], [u'location_ids', u'ilike', u'WH07'], [u'state', u'!=', u'done'], [u'state', u'!=', u'cancel'], [u'state', u'!=', u'draft'], [u'location_ids', u'='', u'WH123']]"
domain = ast.literal_eval(domain)
print(domain)
for a, b, c in domain:
if a == 'location_ids':
print("Found", c)
输出:
[['state', '!=', 'done'], ['state', '!=', 'cancel'], ['state', '!=', 'draft'], ['location_ids', 'ilike', 'WH07'], ['state', '!=', 'done'], ['state', '!=', 'cancel'], ['state', '!=', 'draft'], ['location_ids', '='', 'WH123']]
Found WH07
Found WH123
如果要进行更改,可以使用以下代码示例:
newlist = []
for a, b, c in domain:
if a == 'location_ids':
c = c.replace('WH', 'XX')
newlist.append([a, b, c])
请注意,代码示例中的特殊字符 '
已经被还原为单引号 '
以便在Python中使用。
英文:
Here's code that converts the string back into a Python list of lists, and then parses the list.
import ast
txt = "{u'lang': u'en_US', u'tz': u'Canada/Eastern', u'uid': 1, u'dashboard_merge_domains_contexts': False, u'group_by': [], u'params': {u'action': 315}}"
domain="[[u'state', u'!=', u'done'], [u'state', u'!=', u'cancel'], [u'state', u'!=', u'draft'], [u'location_ids', u'ilike', u'WH07'], [u'state', u'!=', u'done'], [u'state', u'!=', u'cancel'], [u'state', u'!=', u'draft'], [u'location_ids', u'=', u'WH123']]"
domain = ast.literal_eval(domain)
print(domain)
for a,b,c in domain:
if a == 'location_ids':
print("Found", c)
Output:
[['state', '!=', 'done'], ['state', '!=', 'cancel'], ['state', '!=', 'draft'], ['location_ids', 'ilike', 'WH07'], ['state', '!=', 'done'], ['state', '!=', 'cancel'], ['state', '!=', 'draft'], ['location_ids', '=', 'WH123']]
Found WH07
Found WH123
If you want to change it, it would be something like:
newlist = []
for a,b,c in domain:
if a == 'location_ids':
c = c.replace('WH','XX')
newlist.append([a,b,c])
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论