繁体   English   中英

我的 line.split 函数没有按预期工作,我不知道为什么

[英]My line.split function is not working as intended and I'm not sure why

我正在尝试创建一个包含多行的列表,分成四行,并将四行中的每一个放入自己的列表中,但是每当我尝试时,我都会得到一个错误num2 is an Index error list out of range。 我不太确定我的问题是什么。 我希望有人能指出来。

这是我的代码:

item = [
"Ham and Egg Sandwich $(15.75,10.0,44.0)",
"Bacon and Cheese Plate $(9.5,3.0,50.0)",
'Tuna Salad $(12.3,4.0,20.0)',
'Beef Soup $(9.0,2.0,30.0)',
'Spicy Beef Barbeque $(20.0,18.0,20.0)',
'Pork Barbeque $(18.0,12.0,35.0)',
'Oven Chicken Barbeque $(15.0,9.0,50.0)',
'Pulled Beef Barbeque Burger $(25.0,20.0,35.0)',
'House Salad $(5.0,2.5,46.0)',
'Pellegrino $(5.4,3.0,60.0)',
'White Wine $(7.5,5.0,40.0)',
'Red Wine $(11.0,4.0,71.0)'
 ]

for line in item:
   itemInfo = line.split("$")
   itemName = itemInfo[0].strip()
   resplit = itemInfo[1].strip()
   for line in resplit:
     split = line.split(",")
     num1 = split[0].strip()
     num2 = split[1].strip()

请参阅有关原始代码的注释。

item = [
    "Ham and Egg Sandwich $(15.75,10.0,44.0)",
    "Bacon and Cheese Plate $(9.5,3.0,50.0)",
    'Tuna Salad $(12.3,4.0,20.0)',
    'Beef Soup $(9.0,2.0,30.0)',
    'Spicy Beef Barbeque $(20.0,18.0,20.0)',
    'Pork Barbeque $(18.0,12.0,35.0)',
    'Oven Chicken Barbeque $(15.0,9.0,50.0)',
    'Pulled Beef Barbeque Burger $(25.0,20.0,35.0)',
    'House Salad $(5.0,2.5,46.0)',
    'Pellegrino $(5.4,3.0,60.0)',
    'White Wine $(7.5,5.0,40.0)',
    'Red Wine $(11.0,4.0,71.0)'
]

for line in item:
     # line = an entry from the list
     # Example: "Ham and Egg Sandwich $(15.75,10.0,44.0)"
    itemInfo = line.split("$")
     # itemInfo  = list
     # Example ['Ham and Egg Sandwich ', '(15.75,10.0,44.0)']
    itemName = itemInfo[0].strip()
    # itemName = str Example: 'Ham and Egg Sandwich'
    resplit = itemInfo[1].strip()
     # resplit = str Example: '(15.75,10.0,44.0)'
    for line in resplit: # So line = str Example: '(' one character.
        split = line.split(",")
        num1 = split[0].strip() # Is one character '('
        num2 = split[1].strip() # Thus index error, no more characters.

以下代码实现了我理解的目标。

item = [
    "Ham and Egg Sandwich $(15.75,10.0,44.0)",
    "Bacon and Cheese Plate $(9.5,3.0,50.0)",
    'Tuna Salad $(12.3,4.0,20.0)',
    'Beef Soup $(9.0,2.0,30.0)',
    'Spicy Beef Barbeque $(20.0,18.0,20.0)',
    'Pork Barbeque $(18.0,12.0,35.0)',
    'Oven Chicken Barbeque $(15.0,9.0,50.0)',
    'Pulled Beef Barbeque Burger $(25.0,20.0,35.0)',
    'House Salad $(5.0,2.5,46.0)',
    'Pellegrino $(5.4,3.0,60.0)',
    'White Wine $(7.5,5.0,40.0)',
    'Red Wine $(11.0,4.0,71.0)'
]

for line in item:
    # line = an entry from the list
    # Example: "Ham and Egg Sandwich $(15.75,10.0,44.0)"
    itemInfo = line.split("$")
    # itemInfo  = list
    # Example ['Ham and Egg Sandwich ', '(15.75,10.0,44.0)']
    itemName = itemInfo[0].strip()
    # itemName = str Example: 'Ham and Egg Sandwich'
    # itemInfo[1] = str Example: Example: '(15.75,10.0,44.0)'
    numbers = itemInfo[1][1:-1].split(',')  # Drop parentheses and split
    num1 = numbers[0]
    num2 = numbers[1]
    num3 = numbers[2]
    print(f'{itemName=}, {num1=},{num2=},{num3=}')

输出

itemName='Ham and Egg Sandwich', num1='15.75',num2='10.0',num3='44.0'
itemName='Bacon and Cheese Plate', num1='9.5',num2='3.0',num3='50.0'
itemName='Tuna Salad', num1='12.3',num2='4.0',num3='20.0'
itemName='Beef Soup', num1='9.0',num2='2.0',num3='30.0'
itemName='Spicy Beef Barbeque', num1='20.0',num2='18.0',num3='20.0'
itemName='Pork Barbeque', num1='18.0',num2='12.0',num3='35.0'
itemName='Oven Chicken Barbeque', num1='15.0',num2='9.0',num3='50.0'
itemName='Pulled Beef Barbeque Burger', num1='25.0',num2='20.0',num3='35.0'
itemName='House Salad', num1='5.0',num2='2.5',num3='46.0'
itemName='Pellegrino', num1='5.4',num2='3.0',num3='60.0'
itemName='White Wine', num1='7.5',num2='5.0',num3='40.0'
itemName='Red Wine', num1='11.0',num2='4.0',num3='71.0'

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM