将字符串括在单引号内

时间:2018-08-02 07:47:29

标签: python regex

我正在尝试使用单引号{}将大括号' '中的所有单个值包装起来。

示例:

some test {Value1,Value2} more text {Value3} more text {Value4, Value1, Value5, Value9, Value11, Value21}

Above should convert as below:
some test {'Value1','Value2'} more text {'Value3'} more text {'Value4', 'Value1', 'Value5', 'Value9', 'Value11', 'Value21'}

我能够找出模式,但是不确定如何在文件中替换它们。

源文件内容:

2321232~CD IN ('JSS12QQ','OQJS','QE9QAA','HAHSS31A','KAA43A','QA00A')~CD IN 
(JSS12QQ','OQJS','ULOE9Q','HAHSS31A','KAA43A','QA00A','QM34AA','WS2A','JS3QQ')~US~
{QA00A,WS2A,JS3QQ,OQJS,KAA43A,ULOE9Q,QM34AA,HAHSS31A,JSS12QQ}~{KAA43A,QA00A,QM34AA,WS2A,JS3QQ}
~CD IN ('2AA2Q')~CD IN ('2AA2Q')~US~{2AA2Q}~{2AA2Q}~{QM34AA,JSS12QQ}

目标文件内容:

2321232~CD IN ('JSS12QQ','OQJS','QE9QAA','HAHSS31A','KAA43A','QA00A')~CD IN 
(JSS12QQ','OQJS','ULOE9Q','HAHSS31A','KAA43A','QA00A','QM34AA','WS2A','JS3QQ')~US~
{'QA00A','WS2A','JS3QQ','OQJS','KAA43A','ULOE9Q','QM34AA','HAHSS31A','JSS12QQ'}~{'KAA43A','QA00A','QM34AA','WS2A','JS3QQ'}
~CD IN ('2AA2Q')~CD IN ('2AA2Q')~US~{'2AA2Q'}~{'2AA2Q'}~{'QM34AA,JSS12QQ'}

获取模式的代码:

import re

def parse(file_name):
        file = open(file_name)
        file_contents =file.read()
        f = file_contents
        rec=re.findall(r"{.*?}",f)
        print (rec)

parse("rule_engine_temp.csv")

请帮助解释,以便我能够理解和处理其变化。

2 个答案:

答案 0 :(得分:2)

您可以使用

import re
f="some test {Value1,Value2} more text {Value3} more text {Value4, Value1, Value5, Value9, Value11, Value21}"
rec=re.sub(r"{([^{}]*)}", lambda x: "{{'{}'}}".format("', '".join(re.split(r'\s*,\s*', x.group(1)))) ,f)
print(rec)
# => some test {'Value1', 'Value2'} more text {'Value3'} more text {'Value4', 'Value1', 'Value5', 'Value9', 'Value11', 'Value21'}

请参见Python demo

注意:

  • {([^{}]*)}-匹配{,然后将除{}之外的0+个字符捕获到组1中,然后匹配}
  • re.sub将匹配数据对象传递给lambda表达式
  • 第1组的值用\s*,\s*分割-用逗号隔开(用空格隔开(如果间距一致,则可以进行字符串分割操作,类似x.group(1).split(',')),然后用{{1 }}。

答案 1 :(得分:2)

我的尝试

s = """some test {Value1,Value2} more text {Value3} more text {Value4, Value1, Value5, Value9, Value11, Value21}"""

import re

def my_sub(g):
    return '{' + re.sub(r'([a-zA-Z\d]+[a-z\d]+)(,|$)', r"'\1'\2", g[1]) + '}'

s = re.sub(r'{([^{}]+)}', my_sub, s)
print(s)

r'{([^{}]+)}'与花括号(online demo here)中的字符串匹配

然后子调用函数my_sub,在其中我们将逗号添加到值(online demo here)。