zoukankan      html  css  js  c++  java
  • Python笔记:字符串操作


    字符串相关操作

    统计字符串中某个单词的出现的次数

    a = 'test 123 dfg test'
    ## 方法1
    len([i for i in a.split(' ') if i == test])
    
    ## 方法2
    len(a.split('test'))-1
    

    Python提取两个字符串之间的内容

    import re 
    str = '''/begin MEASUREMENT
    100
    LINK
    DISPLAY
    SYMBOL
    /end MEASUREMENT'''
     
    regex = r'/begin MEASUREMENT([\s\S]*)/end MEASUREMENT'
    matches = re.findall(regex, str)
    for match in matches:
        print(match)
    
    import re 
    str = 'test:100      end' 
    regex = r'test:([\s\S]*)/end'
    matches = re.findall(regex, str)
    test = matches[0].strip()
    

    字符删除、替换

    删除空格

    s = ' 123abcd456  '
    # 删除两边的空格
    print(s.strip())
    # 删除右边空格
    print(s.rstrip()) 
    # 删除左边空格
    print(s.lstrip())
    # 删除两边的数字
    print(s.strip(' ').strip('123456'))
    # 删除两边的引号
    s = "'123abcd456'"
    print(s.strip("'"))
    

    分割并去除空格

    string = " hello , world !"
    string = [x.strip() for x in string.split(',')]
    

    将格式化字符转换为字典

    string = "dst='192.168.0.1',src='192.168.1.2'"
    fields = dict((field.split('=') for field in string.split(',')))
    fields = dict(((lambda a:(a[0].strip("'"),a[1].strip("'"))) (field.split('=')) for field in string.split(',')))
    
    >>> fields
    {'dst': "'192.168.0.1'", 'src': "'192.168.1.2'"}
    

    删除(替换)任意位置字符

    s = '11233aabcdd41556'
    # 删除某个特定字符
    print(ss.replace('1', ''))
    # 同时删除不同字符
    import re
    print(re.sub('[1a]', '', s))
    
    --THE END--

    文章标题:Python笔记:字符串操作
    本文作者:hiyo
    本文链接:https://www.cnblogs.com/hiyong/p/14290055.html
    欢迎关注公众号:「测试开发小记」及时接收最新技术文章!

  • 相关阅读:
    初识MySQL
    正则表达式
    多线程
    多进程
    Python基础--模块
    Python基础--异常
    Python基础--面向对象
    Python基础--文件操作
    Python基础--函数
    python3 日期时间差计算
  • 原文地址:https://www.cnblogs.com/hiyong/p/14290055.html
Copyright © 2011-2022 走看看