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

时间:2012-05-14 05:11:16

标签: python python-3.x

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的破折号让我再次看到那条线!

新问题:

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

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

2 个答案:

答案 0 :(得分:3)

备注太长,无法发表评论;

csv.reader()不会将第一行视为标题(仅DictReader具有该行为。)尝试插入一些print语句进行调试 - 查看csv_r包含的内容会很有帮助。


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

你确定你不是要使用方括号作为列表表达式,而不是使用parens作为生成器表达式吗?


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

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


答案 1 :(得分:3)

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

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

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

你循环遍历csv_r中的行,然后使用列表推导来做同样的事情,即reduntant。

这是简化版


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)