python - 在 python 列表中查找字符串模式

标签 python string list

我是 python 新手,需要任何帮助。

我有一个文本文件,我必须对其进行一些清理,即我必须从行中删除一些字符串模式。下面是我到目前为止所做的事情:

f = open("myfile.txt", "r")

mylist = []
for i in f:
    mylist.append(i.strip().split('\t')) #removing newlines '\n'

for i in range(0, len(mylist)): # iterate the list to find the string pattern
    #if "@@" in mylist[i]: # I have tried this as well
    if mylist[i].count('@@') > 0:    
        print(mylist[i])
    else:
        print('NOT')

这段代码没有找到我想要的字符串模式。

最佳答案

import re    
data = [line 
        for line in f.readlines() 
        if re.findall(r'@@', line)] # iterate the list to 

关于python - 在 python 列表中查找字符串模式,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54571245/

相关文章:

python - TypeError : float() argument must be a string or a number, 不是 'function' – Python/Sklearn

python - 并行更新数组?

javascript - 如何删除正则表达式中第一个数字后跟逗号?

python - 查找列表中最大的匹配项

python - childWindow 的 Mainwindow 属性

str(x) 的 Python 默认行为

php - 将文本拆分成句子

python - 列表参数的语法错误

c++ - 如何遍历一个二维列表?

python - 交换嵌套列表中的列 (Python 3)