Python ValueError: 无法将字符串转换为浮点数:'" "'

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/48075861/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-19 18:31:31  来源:igfitidea点击:

ValueError: could not convert string to float: '" "'

pythonstringcsvfloating-pointinteger

提问by vipul gangwar

I have some values is csv file and In csv file some values are numeric and some are string number. Example of csv file:

我有一些值是 csv 文件,在 csv 文件中,一些值是数字,一些是字符串数字。.csv 文件示例:

1,"1151226468812.22",100,1,467,999.00,999.95,15,1,999.00,999.95,998.50,999.95,15,999.01,1396,34,06092016091501.444,1394627.25
2,"1151226468812.11",100,1,467,999.00,1000.00,1605,3,999.00,1000.00,998.50,1000.00,5,999.03,1426,37,06092016091502.111,1424626.50

So I wnated to convert string to float. So here is my code:

所以我想将字符串转换为浮点数。所以这是我的代码:

datareader = csv.reader(datafile, delimiter=",", quoting= csv.QUOTE_NONE)

    names =  []
    names.append("local_timestamp")
    names.append("nse_timestamp")
for row in datareader:
        data = dict()
        data.update(local_timestamp = row[0])
        data.update(nse_timestamp = float(row[1]))

But it return value error.

但它返回值错误。

ValueError: could not convert string to float: '"1151226468812.22"'

回答by scrbbL

The problem is that your string is not just '1151226468812.22', but it is '"1151226468812.22"'. It also contains speech marks ("). This means that before you convert this to a float, you need to remove the leading and trailing speech marks. Luckily, Python has a very handy string method .strip()to do this for you.

问题是您的字符串不仅是'1151226468812.22',而且是'"1151226468812.22"'。它还包含语音标记 ( ")。这意味着在将其转换为浮点数之前,您需要删除前导和尾随语音标记。幸运的是,Python 有一个非常方便的字符串方法.strip()可以为您执行此操作。

string.strip(s)will return a string that has the leading and ending 's'characters removed

string.strip(s)将返回一个's'去除了前导和结束字符的字符串

For example:

例如:

myString = "#hello#".strip("#")

In this code, myStringwould be just 'hello'

在这段代码中,myString将只是'hello'

In this case, you want to strip row[1]of the leading and trailing "characters. You can do this very easily:

在这种情况下,您希望去除row[1]前导和尾随"字符。你可以很容易地做到这一点:

row[1].strip("\"")

回答by Tom de Geus

It is obvious that the double quotes cause the problem, Python can only convert a string of numbers (and the decimal symbol) to a float.

很明显,双引号导致了问题,Python 只能将一串数字(和十进制符号)转换为浮点数。

One way to remove the double-quotes are using a regular expression. This allows you to run the same code, regardless whether the input has the double-quotes or not:

删除双引号的一种方法是使用正则表达式。这允许您运行相同的代码,无论输入是否有双引号:

import re

print(float(re.split(r'[\"]?([0-9\.]*)[\"]?','1151226468812.22')[1]))
print(float(re.split(r'[\"]?([0-9\.]*)[\"]?','"1151226468812.22"')[1]))

Outputs:

输出:

1151226468812.22
1151226468812.22

This regular expression will match:

此正则表达式将匹配:

  • [\"]?a starting double quote, if present (?take care of that).
  • [0-9\.]*a series of numbers or dot-charactes of arbitray length (*takes care of the latter).
  • [\"]?an end double quote, if present.
  • [\"]?一个起始双引号,如果存在(?注意)。
  • [0-9\.]*一系列任意长度的数字或点字符(*处理后者)。
  • [\"]?结束双引号(如果存在)。

It returns a list of length three, of which the second item contains the number. This can then be converted to a float.

它返回一个长度为 3 的列表,其中第二项包含数字。然后可以将其转换为浮点数。

回答by 1313e

Try to use the following:

尝试使用以下内容:

for row in datareader:
    data = dict()
    data.update(local_timestamp = row[0])
    data.update(nse_timestamp = float(row[1].replace('"', '')))

or

或者

for row in datareader:
    data = dict()
    data.update(local_timestamp = row[0])
    data.update(nse_timestamp = float(row[1].strip('"')))

This will remove the double quotes, and now you can convert the string to a float.

这将删除双引号,现在您可以将字符串转换为浮点数。

回答by Danny_ds

The second field in your csv is quoted with ". In csv, having quoted fields does not mean those are strings, but that the field could contain a delimiter, like "123,45".

csv 中的第二个字段用". 在 csv 中,引用字段并不意味着这些是字符串,而是该字段可以包含分隔符,如"123,45".

The right way to read such data is to tell the reader some fields can be quoted:

读取此类数据的正确方法是告诉读者某些字段可以引用:

datareader = csv.reader(datafile, delimiter=',', quotechar='"')

This will return the second field without the quotes and solve your problem.

这将返回没有引号的第二个字段并解决您的问题。

Removing the quotes afterwards not only adds extra work, but can also lead to errors if the field contains a delimiter. For example "123,45"would return "123and 45"as two different fields.

之后删除引号不仅会增加额外的工作,而且如果字段包含分隔符,还会导致错误。例如"123,45"将返回"12345"作为两个不同的字段。