【python】re库 正则的一些过滤和把str拆分成list案例 以及json dict类型

0x01:

部分参考:https://www.cnblogs.com/edwardsun/p/4421773.html

  • match(string[, pos[, endpos]]) | re.match(pattern, string[, flags]):       这个方法将从string的pos下标处起尝试匹配pattern;如果pattern结束时仍可匹配,则返回一个Match对象;如果匹配过程中pattern无法匹配,或者匹配未结束就已到达endpos,则返回None。     pos和endpos的默认值分别为0和len(string);re.match()无法指定这两个参数,参数flags用于编译pattern时指定匹配模式。     注意:这个方法并不是完全匹配。当pattern结束时若string还有剩余字符,仍然视为成功。想要完全匹配,可以在表达式末尾加上边界匹配符'$'。     示例参见2.1小节。
  • search(string[, pos[, endpos]]) | re.search(pattern, string[, flags]):       这个方法用于查找字符串中可以匹配成功的子串。从string的pos下标处起尝试匹配pattern,如果pattern结束时仍可匹配,则返回一个Match对象;若无法匹配,则将pos加1后重新尝试匹配;直到pos=endpos时仍无法匹配则返回None。     pos和endpos的默认值分别为0和len(string));re.search()无法指定这两个参数,参数flags用于编译pattern时指定匹配模式。    
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    # encoding: UTF-8
    import re
     
    # 将正则表达式编译成Pattern对象
    pattern = re.compile(r'world')
     
    # 使用search()查找匹配的子串,不存在能匹配的子串时将返回None
    # 这个例子中使用match()无法成功匹配
    match = pattern.search('hello world!')
     
    if match:
        # 使用Match获得分组信息
        print match.group()
     
    ### 输出 ###
    # world
  • split(string[, maxsplit]) | re.split(pattern, string[, maxsplit]):       按照能够匹配的子串将string分割后返回列表。maxsplit用于指定最大分割次数,不指定将全部分割。    
    1
    2
    3
    4
    5
    6
    7
    import re
     
    p = re.compile(r'\d+')
    print p.split('one1two2three3four4')
     
    ### output ###
    # ['one', 'two', 'three', 'four', '']
  • findall(string[, pos[, endpos]]) | re.findall(pattern, string[, flags]):       搜索string,以列表形式返回全部能匹配的子串。    
    1
    2
    3
    4
    5
    6
    7
    import re
     
    p = re.compile(r'\d+')
    print p.findall('one1two2three3four4')
     
    ### output ###
    # ['1', '2', '3', '4']
  • finditer(string[, pos[, endpos]]) | re.finditer(pattern, string[, flags]):       搜索string,返回一个顺序访问每一个匹配结果(Match对象)的迭代器。    
    1
    2
    3
    4
    5
    6
    7
    8
    import re
     
    p = re.compile(r'\d+')
    for m in p.finditer('one1two2three3four4'):
        print m.group(),
     
    ### output ###
    # 1 2 3 4
  • sub(repl, string[, count]) | re.sub(pattern, repl, string[, count]):       使用repl替换string中每一个匹配的子串后返回替换后的字符串。     当repl是一个字符串时,可以使用\id或\g<id>、\g<name>引用分组,但不能使用编号0。     当repl是一个方法时,这个方法应当只接受一个参数(Match对象),并返回一个字符串用于替换(返回的字符串中不能再引用分组)。     count用于指定最多替换次数,不指定时全部替换。    
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    import re
     
    p = re.compile(r'(\w+) (\w+)')
    s = 'i say, hello world!'
     
    print p.sub(r'\2 \1', s)
     
    def func(m):
        return m.group(1).title() + ' ' + m.group(2).title()
     
    print p.sub(func, s)
     
    ### output ###
    # say i, world hello!
    # I Say, Hello World!
  • subn(repl, string[, count]) |re.sub(pattern, repl, string[, count]):       返回 (sub(repl, string[, count]), 替换次数)。    
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    import re
     
    p = re.compile(r'(\w+) (\w+)')
    s = 'i say, hello world!'
     
    print p.subn(r'\2 \1', s)
     
    def func(m):
        return m.group(1).title() + ' ' + m.group(2).title()
     
    print p.subn(func, s)
     
    ### output ###
    # ('say i, world hello!', 2)
    # ('I Say, Hello World!', 2)

0x02:自己的案例

需要对前端传来的

str数据切分成list

然后传给数据库

原数据:

{"value":"123","isEditing":false,"isActive":false,"isChecked":false},{"value":"2","isEditing":false,"isActive":false,"isChecked":false},{"value":"32","isEditing":false,"isActive":false,"isChecked":false},{"value":"12","isEditing":false,"isActive":false,"isChecked":false},{"value":"12","isEditing":false,"isActive":false,"isChecked":false},{"value":"123","isEditing":false,"isActive":false,"isChecked":false},{"value":"32","isEditing":false,"isActive":false,"isChecked":false},{"value":"23","isEditing":false,"isActive":false,"isChecked":false},{"value":"12","isEditing":false,"isActive":false,"isChecked":false},{"value":"321","isEditing":false,"isActive":false,"isChecked":false},{"value":"21","isEditing":false,"isActive":false,"isChecked":false}]

过滤:

str = text
def checkFilter(keywords,text):
return re.sub('|'.join(keywords),'',str)
keywords = ('\'','"','\[{','\}]','\{')
a = checkFilter(keywords,text)
print (checkFilter(keywords,text))

输出结果:

<class 'str'>
******************
value:123,isEditing:false,isActive:false,isChecked:false},value:2,isEditing:false,isActive:false,isChecked:false},value:32,isEditing:false,isActive:false,isChecked:false},value:12,isEditing:false,isActive:false,isChecked:false},value:12,isEditing:false,isActive:false,isChecked:false},value:123,isEditing:false,isActive:false,isChecked:false},value:32,isEditing:false,isActive:false,isChecked:false},value:23,isEditing:false,isActive:false,isChecked:false},value:12,isEditing:false,isActive:false,isChecked:false},value:321,isEditing:false,isActive:false,isChecked:false},value:21,isEditing:false,isActive:false,isChecked:false

拆分:

b = a.split('},')
print(b)

输出结果:

******************
['value:123,isEditing:false,isActive:false,isChecked:false', 'value:2,isEditing:false,isActive:false,isChecked:false', 'value:32,isEditing:false,isActive:false,isChecked:false', 'value:12,isEditing:false,isActive:false,isChecked:false', 'value:12,isEditing:false,isActive:false,isChecked:false', 'value:123,isEditing:false,isActive:false,isChecked:false', 'value:32,isEditing:false,isActive:false,isChecked:false', 'value:23,isEditing:false,isActive:false,isChecked:false', 'value:12,isEditing:false,isActive:false,isChecked:false', 'value:321,isEditing:false,isActive:false,isChecked:false', 'value:21,isEditing:false,isActive:false,isChecked:false']
******************

源码:

import re
input = open('I:\\python_test\\json_mysql\\jsondata.txt','r')
text=input.read()
print (text)
print ('******************')
print (type(text))
print ('******************')
str = text
def checkFilter(keywords,text):
return re.sub('|'.join(keywords),'',str)
keywords = ('\'','"','\[{','\}]','\{')
a = checkFilter(keywords,text)
print (checkFilter(keywords,text))
print ('******************') b = a.split('},')
print(b)
print ('******************')
print (type(b))
print ('******************')
print (b[0])
print ('******************')
str1 = b[0]
b1 = str1.split(':') print (b1[0])

输出:

[{"value":"","isEditing":false,"isActive":false,"isChecked":false},{"value":"","isEditing":false,"isActive":false,"isChecked":false},{"value":"","isEditing":false,"isActive":false,"isChecked":false},{"value":"","isEditing":false,"isActive":false,"isChecked":false},{"value":"","isEditing":false,"isActive":false,"isChecked":false},{"value":"","isEditing":false,"isActive":false,"isChecked":false},{"value":"","isEditing":false,"isActive":false,"isChecked":false},{"value":"","isEditing":false,"isActive":false,"isChecked":false},{"value":"","isEditing":false,"isActive":false,"isChecked":false},{"value":"","isEditing":false,"isActive":false,"isChecked":false},{"value":"","isEditing":false,"isActive":false,"isChecked":false}]
******************
<class 'str'>
******************
value:123,isEditing:false,isActive:false,isChecked:false},value:2,isEditing:false,isActive:false,isChecked:false},value:32,isEditing:false,isActive:false,isChecked:false},value:12,isEditing:false,isActive:false,isChecked:false},value:12,isEditing:false,isActive:false,isChecked:false},value:123,isEditing:false,isActive:false,isChecked:false},value:32,isEditing:false,isActive:false,isChecked:false},value:23,isEditing:false,isActive:false,isChecked:false},value:12,isEditing:false,isActive:false,isChecked:false},value:321,isEditing:false,isActive:false,isChecked:false},value:21,isEditing:false,isActive:false,isChecked:false
******************
['value:123,isEditing:false,isActive:false,isChecked:false', 'value:2,isEditing:false,isActive:false,isChecked:false', 'value:32,isEditing:false,isActive:false,isChecked:false', 'value:12,isEditing:false,isActive:false,isChecked:false', 'value:12,isEditing:false,isActive:false,isChecked:false', 'value:123,isEditing:false,isActive:false,isChecked:false', 'value:32,isEditing:false,isActive:false,isChecked:false', 'value:23,isEditing:false,isActive:false,isChecked:false', 'value:12,isEditing:false,isActive:false,isChecked:false', 'value:321,isEditing:false,isActive:false,isChecked:false', 'value:21,isEditing:false,isActive:false,isChecked:false']
******************
<class 'list'>
******************
value:123,isEditing:false,isActive:false,isChecked:false
******************
value

2910.1.14-------------------------------------------------------------------------------------------------------

拼接dict类型 json

----------------------------------------------------------------------------------------------------------------

b = a.split('},')
print(b)
print ('******************')
print (type(b))
print ('******************')
print (b[0])
print (type(b[0]))
print ('******************')
lines_json = b[0].replace(',', '","')#替换字符;;;;;具体b[i] for输出到数据库
lines_json1 = lines_json.replace(':','":"')
lines_json2 = ('{"'+lines_json1+'"}')
print (lines_json2)
print ('******************')
j = json.loads(lines_json2)
print (j)
print (j["value"])
print(type(j))  

输出:

******************
<class 'list'>
******************
value:123,isEditing:false,isActive:false,isChecked:false
<class 'str'>
******************
{"value":"","isEditing":"false","isActive":"false","isChecked":"false"}
******************
{'isActive': 'false', 'value': '', 'isEditing': 'false', 'isChecked': 'false'}
123
<class 'dict'>

-------

上一篇:VisualStudioCode创建的asp.net core项目部署到linux,使用nginx代理


下一篇:python运算符重载(二)