CSV 的第一行被忽略,csv.reader() |菜单和列表 - 从简单的input()菜单和循环访问CSV中的不同行



lol.txt如下:

1,2,3n
n
4,5,6n
n
AwesomeSauce,12.3,10n

我正在使用的代码:

import csv
NumberAdult = input("Please enter the number of adults: ")
NumberAdult = int(NumberAdult)
NumberChild = input("Please enter the number of children: ")
NumberChild = int(NumberChild)
n = 0
with open ("lol.txt",'rt', newline = 'n') as f:
    csv_r = (line for line in csv.reader(f) if line)
    for row in csv_r:
        entries = [(name, float(price1), float(price2)) for name, price1, price2 in csv_r]
        for index, entry in enumerate(entries):
            price1 = float(entry[1])
            price2 = float(entry[2])
            print ("%i. %17s - %5.2f / %5.2f" % (index, entry[0], price1, price2))
choice = int(input("Which package would you like?: "))
packageChoice = (entries[choice])
for row in packageChoice:
    name = entry[0]
    AdultPrice = float(entry[1])
    ChildPrice = float(entry[2])
price = AdultPrice*NumberAdult + ChildPrice*NumberChild
print(name, price)

输出:

请输入成人人数:2
请输入儿童人数:1
0. 4 - 5.00/6.00
1. 棒酱 - 12.30/10.00
您想要哪种套餐?: 1
真棒酱 34.6

这意味着它忽略了 lol.txt 的第一行 - 1,2,3n - 因为csv.reader()似乎将此行视为字段名而不是数据。

有没有办法解决这个问题?使用csv.dictreader()或其他东西来分配独立于文件本身的字段名称?

编辑:没关系csv.reader(),它不会将它们视为字段名称。所以看起来问题出在本节中:

with open ("lol.txt",'rt', newline = 'n') as f:
    csv_r = (line for line in csv.reader(f) if line)
    for row in csv_r:

现在,我不知道在这里该怎么做 - 这是我最接近工作的脚本。任何提示,搜索词,任何东西?

最终编辑:没关系,现在一切都有效!我有一个我忘记的循环:

for row in csv_r:
   entries = [(name, float(price1), float(price2)) for name, price1, price2 in csv_r]

这导致它跳过第一行。感谢 freenode 上 #python 的破折号让我再次看到那条线!

新问题:

import csv
NumberAdult = input("Please enter the number of adults: ")
NumberAdult = int(NumberAdult)
NumberChild = input("Please enter the number of children: ")
NumberChild = int(NumberChild)
n = 0
with open ("lol.txt",'rt', newline = 'n') as f:
    csv_r = (line for line in csv.reader(f) if line)
    entries = [(name, float(price1), float(price2)) for name, price1, price2 in csv_r]
    for index, entry in enumerate(entries):
        price1 = float(entry[1])
        price2 = float(entry[2])
        print ("%i. %17s - %5.2f / %5.2f" % (index, entry[0], price1, price2))
choice = int(input("Which package would you like?: "))
packageChoice = (entries[choice])
for row in packageChoice:
    name = entry[0]
    AdultPrice = float(entry[1])
    ChildPrice = float(entry[2])
price = AdultPrice*NumberAdult + ChildPrice*NumberChild
print(name, price)

唯一的"选项"(无论您输入什么选项(是 2.,因为它是列表entries的最后一行。

>>>
Please enter the number of adults: 2
Please enter the number of children: 1
0.                 1 -  2.00 /  3.00
1.                 4 -  5.00 /  6.00
2.      AwesomeSauce - 12.30 / 10.00
Which package would you like?: 1
AwesomeSauce 34.6
>>>

现在,我相当确定问题出在这里:

csv_r = (line for line in csv.reader(f) if line)
entries = [(name, float(price1), float(price2)) for name, price1, price2 in csv_r]
for index, entry in enumerate(entries):
或者,我

应该为菜单的结果做另一个类似的部分,我不确定。我将努力实现两者以尝试对其进行故障排除。

太长而无法在评论中的评论;

csv.reader()不会将第一行视为标题(只有DictReader具有这种行为。尝试插入一些用于调试的打印语句 - 查看csv_r包含的内容会很有帮助。


csv_r = (line for line in csv.reader(f) if line)

您确定您不是要对列表表达式使用方括号,而不是对生成器表达式使用括号吗?


for row in csv_r:
        entries = [(name, float(price1), float(price2)) for name, price1, price2 in csv_r]

您确定要为csv_r中的每一行实现一次列表entries吗?当然,您只需要执行此操作一次吗?


您错误地使用了 csv 模块。它返回一个生成器,因此后续调用返回更多行。

实际上,您正在生成相同生成器的列表。

您似乎转换/检查了两次数据

您正在循环访问csv_r中的行,然后对列表理解进行签名以执行相同的操作,这是多余的。

这是一个简化版本


import csv
NumberAdult = input("Please enter the number of adults: ")
NumberAdult = int(NumberAdult)
NumberChild = input("Please enter the number of children: ")
NumberChild = int(NumberChild)
n = 0
with open ("lol.txt") as f:
    csv_r = csv.reader(f)
    entries=[]
    for row in csv_r:
        #TODO:do checks here
        entries.append((row[0], float(row[1]), float(row[2])))
for index, entry in enumerate(entries):
    price1 = entry[1]  #do you need this?
    price2 = entry[2]  #do you need this?
    print ("%i. %17s - %5.2f / %5.2f" % (index, entry[0], price1, price2))
choice = int(input("Which package would you like?: "))
packageChoice = (entries[choice])
for row in packageChoice:
    name = entry[0]
    AdultPrice = float(entry[1])
    ChildPrice = float(entry[2]) 
price = AdultPrice*NumberAdult + ChildPrice*NumberChild
print(name, price) 

最新更新