简体   繁体   English

如何修复 ValueError :无法将字符串转换为浮点数:在 Python 中

[英]how to fix ValueError :could not convert string to float: in Python

i have a python script that read from CSV file and check if the records meet the conditions.我有一个 python 脚本,它从 CSV 文件中读取并检查记录是否满足条件。

  • if yes the system display the result如果是,系统显示结果
  • if no the system raise Exception based on the Error.如果没有,系统会根据错误引发异常。

the csv file includes a filed that has float values but some of these records may not have any value so will be empty. csv 文件包含一个具有浮点值的字段,但其中一些记录可能没有任何值,因此将为空。

the problem is if the cell is empty the system display this ValueError :问题是如果单元格为空,系统会显示此 ValueError :

could not convert string to float: 

and not the Exception that i wrote it.而不是我写的例外。

 raise Exception("this Record has empty value")
  • row[0]==> Date type Date行[0]==>日期类型日期
  • row[10]==> wind speed type float row[10]==> 风速型浮标
  • row[11]==> fog type boolean行[11]==>雾类型布尔值

code:代码:

import csv

mydelimeter = csv.excel()
mydelimeter.delimiter=";"
myfile = open("C:/Users/test/Documents/R_projects/homework/rdu-weather-history.csv")

# read the first line in the opened file ==> Header
myfile.readline()


myreader=csv.reader(myfile,mydelimeter)
mywind,mydate=[],[]
minTemp, maxTemp = [],[]
fastwindspeed, fog=[],[]

'''
create a variable that handle values of the 3 fields ==> Date - fastest5secwindspeed - fog
 and display the result where  
     fog ==> Yes    and highest speed  more than 10.
'''
for row in myreader:
    try:
        if row[11] =="Yes":
            if float(row[10]) < 10.0:
                raise Exception( 'the wind speed  is below 10 mph in ' + row[0] )
            if row[10] in (None, ""):
                raise Exception("this Record has empty value")

            print(row[0],row[10],row[11])
    except Exception as e:
        print("{}".format(e))

myfile.close()

You can change the order of your raises, also you should be handling the possibility of a non-float in that column:你可以改变你的加注顺序,你也应该处理该列中非浮动的可能性:

import csv

mydelimeter = csv.excel()
mydelimeter.delimiter=";"
myfile = open("C:/Users/test/Documents/R_projects/homework/rdu-weather-history.csv")

# read the first line in the opened file ==> Header
myfile.readline()


myreader=csv.reader(myfile,mydelimeter)
mywind,mydate=[],[]
minTemp, maxTemp = [],[]
fastwindspeed, fog=[],[]

'''
create a variable that handle values of the 3 fields ==> Date - fastest5secwindspeed - fog
 and display the result where  
     fog ==> Yes    and highest speed  more than 10.
'''
for row in myreader:
    try:
        if row[11] =="Yes":
            if row[10] in (None, ""):
                raise Exception("this Record has empty value")
            try:
                if float(row[10]) < 10.0:
                    raise Exception( 'the wind speed  is below 10 mph in ' + row[0] )
            except ValueError:
                raise Exception('This Column expected to have a float has a non-float instead")

            print(row[0],row[10],row[11])
    except Exception as e:
        print("{}".format(e))

myfile.close()

I also faced this issue during my project work.我在项目工作期间也遇到过这个问题。 I just did like below:-我只是喜欢下面:-

  if(len(row[10]) > 0):
      wind_speed = flot(row[10])

暂无
暂无

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

相关问题 如何修复“ValueError:无法将字符串转换为浮点数:&#39;East&#39;”(Python) - How to fix "ValueError: could not convert string to float: 'East'" (Python) 如何使用tkinter修复Python中的“ ValueError:无法将字符串转换为float:” - How to fix “ValueError: could not convert string to float:” in Python with tkinter 如何修复 ValueError: could not convert string to float: in python - How to fix the ValueError: could not convert string to float: in python 如何修复“ValueError:无法将字符串转换为浮点数”? - How to fix “ValueError: could not convert string to float”? 如何修复“ValueError:无法将字符串转换为浮点数” - How to fix “ValueError: could not convert string to float” 如何修复此错误:ValueError:无法将字符串转换为浮点数:'A' - How to fix this error: ValueError: could not convert string to float: 'A' 如何修复错误 ValueError: could not convert string to float in a NLP project in python? - how to fix the error ValueError: could not convert string to float in a NLP project in python? ValueError:无法将字符串转换为浮点数:&#39;F&#39; python - ValueError: could not convert string to float: 'F' python ValueError:无法将字符串转换为float&#39;jpg&#39;Python - ValueError: could not convert string to float 'jpg' Python Python中的“ValueError:无法将字符串转换为float” - “ValueError: could not convert string to float” in Python
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM