分隔符后有一个循环键。如何保存?

问题描述 投票:0回答:1
---------------------------
CompanyID: 000000000000
Pizza: 2   3.15    6.30
spaghetti:  1   7    7
ribye: 2  40  80
---------------------------
CompanyID: 000000000001
burger: 1   3.15    6.30
spaghetti:  1   7    7
ribye: 2  40  80
--------------------------

我正在对行列表进行for循环。每行都是列表的一项。寻找用户输入时,我需要保留companyID。

虽然这将打印变量x = True。我无法使用公司ID进行打印。

a='-'
for line in lines:
    if a in line:
        companyID= next(line)

    if product in line:
        x=True
TypeError: 'str' object is not an iterator
python-3.x list loops search
1个回答
0
投票

也许您正在寻找这样的东西:

a='-'
for index, line in enumerate(lines):
    if a in line:
        companyID = lines[index + 1].split()[1]

    if product in line:
        x=True
© www.soinside.com 2019 - 2024. All rights reserved.