vipul gangwar
vipul gangwar

Reputation: 313

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

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

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"'

Upvotes: 6

Views: 163666

Answers (4)

Danny_ds
Danny_ds

Reputation: 11406

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".

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 "123 and 45" as two different fields.

Upvotes: 1

scrbbL
scrbbL

Reputation: 456

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.

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

For example:

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

In this code, myString would be just 'hello'

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

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

Upvotes: 11

1313e
1313e

Reputation: 1222

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.

Upvotes: 0

Tom de Geus
Tom de Geus

Reputation: 5965

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.

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.

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

Upvotes: 0

Related Questions