Py学习  »  Python

如果列表中的某个项在python中有特定的字符串,则如何提取该项?

rachelvsamuel • 5 年前 • 1666 次点击  

我有一个列表,每个条目中有许多条目,用 [] .例如。,

['1', 'pbkdf2_sha256$100000$sk3ONL23432fsdgUsHM62xa9XJHL+LkJHhK3cFGj8LYWGtOd8HC7Hs=',
'2018-09-25 19:32:41', '0', '', '', 'bob@trellis.law', 'Bob', 'Simon', 
'bob@trellis.law', '1', '0', '2016-12-30 17:43:41', 'Bob Simon', 'Bob', '0', '1', 
'', '[]', '', '0', '1', '0', '1', '', '', '1', '14', '191', '1', '0', '1', '0', '', 
'', '', '0']

我想找到包含这个regex的条目,然后捕获 整个的 变量中的行:

r = re.compile(r'\w+\+\d+@trellis\.law')

我尝试过失败:

def import_csv(csv_file):
    name_entries = []
    with open(csv_file) as csvfile:
        reader = csv.reader(csvfile)
        name_entries.append(list(reader))
    return name_entries


def exclude_regex_users(name_entries):
    pulled_names = []
    r = re.compile(r'\w+\+\d+@trellis\.law')

    reader = csv.reader(name_entries)

    for read in reader:
        n = r.match(read)
        if n:
            pulled_names.append(n.group())

    print(pulled_names)

我得到一个 _csv.Error: iterator should return strings, not list (did you open the file in text mode?) 是的。

啊。

Python社区是高质量的Python/Django开发社区
本文地址:http://www.python88.com/topic/46352
 
1666 次点击  
文章 [ 1 ]  |  最新文章 5 年前