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
  • 相关阅读:
    关于我的新博客
    我今天申请了blog!
    Win XP / Win 7上配置eclipse+CDT+MinGW,和相关问题的解决办法
    Android app Installation error: INSTALL_FAILED_CONFLICTING_PROVIDER
    用adb安装程序,和在电脑和设备之间传文件
    eclipse 断点调试快捷键(转)
    找不到设备,device not found错误
    eclipse里面的C printf 先输出到缓冲区
    Eclipse Outline 图示
    如何获得Android系统版本
  • 原文地址:https://www.cnblogs.com/liujiaxin2018/p/15706052.html
Copyright © 2011-2022 走看看