python - 循环遍历列表时提前阅读
问题描述
我有一个文本文件列表,如下所示:
page_text_list = ['.............', '.............','name: bill','name: bob','address: 123 main st','name : tim','address: 124' ,'main st','name:', '.......']
如果我在字符串中找到“名称:”,我想提前阅读以获取该名称的地址。但是,正如您所看到的,模式是不一致的,并且并不总是可以假设下一行包含完整地址。
我想使用一个简单的循环遍历列表
for line in page_text_list:
但这似乎不适合这项工作。这里最好的方法是什么?
解决方案
假设您想要获取该行之后name: ...
直到下一name: ...
行的所有行的列表,您可以这样做:
from itertools import dropwhile, takewhile
page_text_list = ['.............', '.............','name: bill','name: bob','address: 123 main st','name: tim','address: 124' ,'main st','name:', '.......']
def get_address(name):
# we drop all the lines who aren't 'name: bob'
it = dropwhile(lambda line: line != "name: " + name, page_text_list)
try:
next(it) # we drop the 'name: bob' line
except StopIteration: # if the name wasn't found, we exhausted the iterator
pass
# we return all the following lines, while they don't contain 'name:'
return list(takewhile(lambda line:"name:" not in line, it))
输出:
print(get_address('bill')) # no address
# []
print(get_address('dude')) # not in our list
# []
print('\n'.join(get_address('tim')))
# address: 124
# main st
推荐阅读
- objective-c - 以编程方式在 NSMatrix 中选择一个单元格自动使该单元格可见
- postman - 无法在邮递员中执行 if 语句
- java - 范围:添加类名以测试成功
- python - 在 Python 中使用 cumprod() 计算权益曲线
- c++ - 定义模板结构的特征时出现不完整的类型错误
- python - 当您尝试将数据类型分配给 python 中的输入时发生错误
- python - pandas groupby ffill bfill 需要中间 groupby?
- javascript - 如何设置/修改 contenteditable div 的值?
- import - 使用文本流批量插入
- sql-server - 连接到 Microsoft SQL Server 的命名实例