python 中从文本中提取包含指定字符的数据

 

1、

[root@PC1 test]# ls
a.txt  test.py
[root@PC1 test]# cat a.txt
u r d
s f e
a d e
x v m
e f x
e r d
z d v
[root@PC1 test]# cat test.py          ## 提取程序,提取文本中包含e的数据
#!/usr/bin/python

in_file = open("a.txt", "r")
out_file = open("result.txt", "w")

lines = in_file.readlines()

for i in lines:
    if "e" in i:
        out_file.write(i)

in_file.close()
out_file.close()
[root@PC1 test]# python test.py        ## 执行程序
[root@PC1 test]# ls
a.txt  result.txt  test.py
[root@PC1 test]# cat result.txt        ## 查看结果
s f e
a d e
e f x
e r d

 

posted @ 2022-05-31 22:15  小鲨鱼2018  阅读(402)  评论(0编辑  收藏  举报