0

lol.txt 如下:

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

我正在使用的代码:

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. AwesomeSauce - 12.30 / 10.00
您想要哪种套餐?:1
AwesomeSauce 34.6

这意味着它忽略了 lol.txt 的第一行1,2,3\n- 因为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 的 dash 让我再次看到那条线!

新问题:

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.,因为它是通过 list 放入的最后一行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):

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

4

2 回答 2

3

评论太长,无法进入评论;

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]

您确定要为中entries的每一行实现一次列表csv_r吗?当然你只需要这样做一次吗?


于 2012-05-14T05:35:11.027 回答
3

您错误地使用了 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) 
于 2012-05-14T06:05:31.503 回答