pyhon创造一个在文献各行中搜索配合子串的天生器

pyhon创造一个在文献各行中搜索配合子串的天生器

创造一个在文献各行中搜索配合子串的天生器

>>> def filematch(filename, substr):

with open(filename, 'r') as f:

for line in f:

if substr in line:

yield line

>>> for line in open('Data/portfolio.csv'):

print(line, end='')

name,shares,price

"AA",100,32.20

"IBM",50,91.10

"CAT",150,83.44

"MSFT",200,51.23

"GE",95,40.37

"MSFT",50,65.10

"IBM",100,70.44

>>> for line in filematch('Data/portfolio.csv', 'IBM'):

print(line, end='')

"IBM",50,91.10

"IBM",100,70.44

>>>

分享到 :
npm常用训令
上一篇 2021-03-30

Leave a Reply

Your email address will not be published. Required fields are marked *