在Python中查找字符串中的所有模式出现并进行替换。

huangapple go评论68阅读模式
英文:

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])

huangapple
  • 本文由 发表于 2023年8月4日 02:20:17
  • 转载请务必保留本文链接:https://go.coder-hub.com/76830704.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定