python处理字符串

代码找到包含字符串abc的行(可进行再处理)

file = Path(
    '/Users/soymilk/Documents/groovys/jenkins/prod-skb-web-vue-git.groovy')
# with open(Path(file), 'r') as f:
#     print(f.read())
with open(Path(file), 'r') as f:
    for line in f.readlines():
        if 'abc' in line:
            print(line)

找到所有包含字符串abc的行(生成列表)

file = Path(
    '/Users/soymilk/Documents/groovys/jenkins/prod-skb-web-vue-git.groovy')
l3 = []
# with open(Path(file), 'r') as f:
#     print(f.read())
with open(Path(file), 'r') as f:
    for line in f.readlines():
        if 'abc' in line:
            l3.append(line)

f.readlines() 是列表,f.read()是一整个文本

 

简单写法

data=['aaaaaFFa','ccccccc','bbbbFF']
l = [s for s in data if 'FF' in s]

 

posted @ 2022-05-30 16:03  豆浆D  阅读(35)  评论(0编辑  收藏  举报