zoukankan      html  css  js  c++  java
  • python中提取包含指定字符的行、提取以指定字符开头、指定字符结尾的行

    1、测试数据

    root@PC1:/home/test# ls
    test.txt
    root@PC1:/home/test# cat test.txt   ## 测试数据
    w r s f
    a s d g
    z c w d
    a d g t
    z c e w

    2、提取包含w的行

    root@PC1:/home/test# ls
    test.py  test.txt
    root@PC1:/home/test# cat test.txt   ## 测试数据
    w r s f
    a s d g
    z c w d
    a d g t
    z c e w
    root@PC1:/home/test# cat test.py   ## 脚本
    fp = open("test.txt", "r")      ## 打开文件
    sample = fp.readlines()
    
    file = open("result.txt", "w")   ## 指定写入文件
    
    for line in sample:           ## 对每行进行遍历
        if "w" in line:            ## 利用if条件进行判断
            file.write(line)
    
    fp.close()
    file.close()
    root@PC1:/home/test# python3 test.py    ## 执行命令
    root@PC1:/home/test# ls   
    result.txt  test.py  test.txt
    root@PC1:/home/test# cat result.txt   ## 查看结果
    w r s f
    z c w d
    z c e w

    2、同时提取包含w和t的行:

    root@PC1:/home/test# ls
    test.py  test.txt
    root@PC1:/home/test# cat test.txt
    w r s f
    a s d g
    z c w d
    a d g t
    z c e w
    root@PC1:/home/test# cat test.py
    fp = open("test.txt", "r")
    sample = fp.readlines()
    
    file = open("result.txt", "w")
    
    for line in sample:
        if "w" in line or "t" in line:     ## 同时提取包含w 或者 包含 t的行
            file.write(line)
    
    fp.close()
    file.close()
    root@PC1:/home/test# python3 test.py
    root@PC1:/home/test# ls
    result.txt  test.py  test.txt
    root@PC1:/home/test# cat result.txt    ## 查看结果
    w r s f
    z c w d
    a d g t
    z c e w

    3、提取以w开头的行

    root@PC1:/home/test# ls
    test.py  test.txt
    root@PC1:/home/test# cat test.txt    ## 测试数据
    w r s f
    a s d g
    z c w d
    a d g t
    z c e w
    root@PC1:/home/test# cat test.py    ## python脚本
    fp = open("test.txt", "r")
    sample = fp.readlines()
    
    file = open("result.txt", "w")
    
    for line in sample:
        if line.startswith("w"):    ## 提取以w开头的行
            file.write(line)
    
    fp.close()
    file.close()
    root@PC1:/home/test# python3 test.py   ## 执行脚本
    root@PC1:/home/test# ls
    result.txt  test.py  test.txt
    root@PC1:/home/test# cat result.txt      ## 查看结果
    w r s f

    4、同时提取以w开头 或者 以z开头的行

    root@PC1:/home/test# ls
    test.py  test.txt
    root@PC1:/home/test# cat test.txt
    w r s f
    a s d g
    z c w d
    a d g t
    z c e w
    root@PC1:/home/test# cat test.py
    fp = open("test.txt", "r")
    sample = fp.readlines()
    
    file = open("result.txt", "w")
    
    for line in sample:
        if line.startswith("w") or line.startswith("z"):    ## 同时提取以w开头或者以z开头的行
            file.write(line)
    
    fp.close()
    file.close()
    root@PC1:/home/test# python3 test.py
    root@PC1:/home/test# ls
    result.txt  test.py  test.txt
    root@PC1:/home/test# cat result.txt         ## 查看结果
    w r s f
    z c w d
    z c e w

    5、提取以w结尾的行

    root@PC1:/home/test# ls
    test.py  test.txt
    root@PC1:/home/test# cat test.txt   ## 测试数据
    w r s f
    a s d g
    z c w d
    a d g t
    z c e w
    root@PC1:/home/test# cat test.py
    fp = open("test.txt", "r")
    sample = fp.readlines()
    
    file = open("result.txt", "w")
    
    for line in sample:
        if line.endswith("w\n"):   ## 每行的末尾是换行符,所以提取最后字符为w的行为 w\n
            file.write(line)
    
    fp.close()
    file.close()
    root@PC1:/home/test# python3 test.py
    root@PC1:/home/test# ls
    result.txt  test.py  test.txt
    root@PC1:/home/test# cat result.txt   ## 查看结果
    z c e w

    6、提取以z开头同时又以g结尾的行

    root@PC1:/home/test# ls
    test.py  test.txt
    root@PC1:/home/test# cat test.txt
    w r s f
    z s d g
    z c w d
    z d g g
    z c e w
    root@PC1:/home/test# cat test.py
    fp = open("test.txt", "r")
    sample = fp.readlines()
    
    file = open("result.txt", "w")
    
    for line in sample:
        if line.startswith("z") and line.endswith("g\n"):  ## 提取以z开头,同时以g结尾的行
            file.write(line)
    
    fp.close()
    file.close()
    root@PC1:/home/test# python3 test.py
    root@PC1:/home/test# cat result.txt        ## 查看结果
    z s d g
    z d g g
  • 相关阅读:
    openpyxl python操作Excel表格,
    ansible剧本
    ansible基础知识(二)
    ansible基础知识
    Flask-Migrate
    Python3-笔记-B-003-数据结构-元组tuple( )
    Python3-笔记-B-002-数据结构-字典dict{ }
    Python3-笔记-B-001-数据结构-列表list[ ]
    Python3-笔记-A-001-基本语法
    cordova 学习链接
  • 原文地址:https://www.cnblogs.com/liujiaxin2018/p/15706052.html
Copyright © 2011-2022 走看看