繁体   English   中英

使用csv.dictreader的浮点函数错误字符串

[英]string to float function error using csv.dictreader

我正在使用以下2行csv文件作为初始测试。

milk_price_per_cup,cream_price_per_cup,sugar_price_per_cup,vanilla_extract_price_per_cup,

0.70,1.65,1.0,30,

然后尝试运行以下代码:

import csv

input_file = csv.DictReader(open("ingredient_prices.csv"))

milk_price = None
cream_price = None
sugar_price = None
vanilla_price = None


for row in input_file:
    milk_price = float(row["milk_price_per_cup"])
    cream_price = row["cream_price_per_cup"]
    sugar_price = row["sugar_price_per_cup"]
    vanilla_price = row["vanilla_extract_price_per_cup"]  


    print "The milk price is %d, the cream price is %s, the sugar price is %s, and the vanilla price is %s." % (milk_price, cream_price, sugar_price, vanilla_price)

不幸的是,牛奶价格的输出不是浮动的,而是0而不是0.7。...这是怎么回事? 使用Python 2.7.6

字符串格式的%d表示整数十进制。 如果要浮动,则应使用%f

此处阅读有关字符串格式选项的更多信息

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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