首页 > 解决方案 > 是什么导致了这个错误(ValueError: could not convert string to float: 'High')?

问题描述

我不知道如何修复错误(ValueError:无法将字符串转换为浮点数:'High')。任何帮助将非常感激。

high = 0

with open('file.csv', 'r') as csv_file:
    csv_reader = csv.reader(csv_file)

    for line in csv_reader:

            if float(line[2]) > high:
                high = float(line[2])

标签: pythonstringcsv

解决方案


我猜你的 CSV 有一个标题行,所以第一行的列值是字符串“High”,它不能转换为数字。在这种情况下,只需在循环next(csv_reader)之前调用即可跳过第一行。for


推荐阅读