简体   繁体   中英

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

I'm trying to make a list with multiple lines, split in four, and have each of the four go into its own list, but whenever I try, I get an error of num2 is an Index error list out of range. I'm not quite sure what my problem is. I was hoping someone could point it out.

Here is my code:

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()

See the comments in line regarding the original code.

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.

Following code accomplishes the objective as I understand it.

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=}')

Output

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'

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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