我的line.split函数无法按预期工作,我不确定为什么
我正在尝试用多行列出一个列表,分为四个,并将四个中的每一个分为列表,但是每当我尝试时,我都会获得num2
的错误是索引错误列表超出范围。我不太确定我的问题是什么。我希望有人可以指出。
这是我的代码:
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()
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("quot;)
itemName = itemInfo[0].strip()
resplit = itemInfo[1].strip()
for line in resplit:
split = line.split(",")
num1 = split[0].strip()
num2 = split[1].strip()
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
请参阅有关原始代码的评论。
遵循代码实现了我理解的目标。
输出
See the comments in line regarding the original code.
Following code accomplishes the objective as I understand it.
Output