【发布时间】:2020-12-11 07:11:11
【问题描述】:
我有 export.csv 文件,里面有数据
name,total
candy,10
ice cream,15
potatoe chips,20
当我使用此代码读取 .csv 文件时
with open('files/export.csv','r') as file_csv:
read= list(csv.reader(file_csv))
for row in read:
print(row)
原来total列中的数据是字符串数据类型而不是浮点数。我希望输出是这样的:
"name","total"
"candy",10
"ice cream",15
"potatoe chips",20
所以我可以求和并获取总列数据。
【问题讨论】:
-
float内置函数可以将字符串转换为浮点数,只要它是 Python 的字面浮点格式。