简体   繁体   中英

Can't convert string number value to float

I am working with a CSV file that contains 2 columns, the first a date column with format yyyymmdd, the second a string value that is in the format of a float, 0.0 for example.

import csv
file = open('Precipitation.csv')
date_file = csv.reader(file)

dec = 0.0

for k in date_file:
    full_date = k[0]
    month = full_date[4:-2]
    rain_amount = k[1]
    float(month)

    if month == "12":
         float(rain_amount)
         dec += rain_amount

print(dec)

I keep checking the type of rain_amount by using type(rain_amount), and it keeps registering as "str." What am I doing wrong? I keep trying to check and do different things but I can't find out the problem. I am definitely doing something wrong, but I have been looking at this too long to figure it out. Help?

float() doesn't modify a variable in-place, it returns the new typed value. Assign it back:

rain_amount = float(rain_amount)

The float() function does not change its argument - it calculates the appropriate floating-point number required and returns that as a new object, so you have to save that somewhere if you want to use it. If you like, you can combine the float() call with the updating step, eg:

dec += float(rain_amount)

You may be wondering why Python works like that.

As mentioned in the Python Data model docs

An object's type determines the operations that the object supports (eg, “does it have a length?”) and also defines the possible values for objects of that type. The type() function returns an object's type (which is an object itself). Like its identity, an object's type is also unchangeable.

[Footnote] It is possible in some cases to change an object's type, under certain controlled conditions. It generally isn't a good idea though, since it can lead to some very strange behaviour if it is handled incorrectly.

So in general it's best to think of the type of an object as unchangeable. We may casually speak of converting a string to a float, but that's really just a sloppy way of saying that we are creating a float object with a numerical value that corresponds to the string object's contents.

Not only can't you change an object's type, for many basic Python object types you can't even change their value! Such objects are called immutable ; common immutable types include all numeric types, strings, and tuples. Apart from tuples, most built-in container types (lists, dictionaries and sets (but not frozensets)) are mutable, meaning you can modify their contents.

So if you do

s = 'Sam'
s += 'my'

You aren't actually changing the string object s : you can't because it's immutable. Instead, a new string object is created that contains 'Sammy' , that new object is then bound to the name s , and the old object that contained 'Sam' is discarded.

Similarly, if you do

a = 1
a += 1

a new integer object is created that has the value of a+1 and that new object is bound to the name a .

For more info on this topic, please see Facts and myths about Python names and values by SO veteran Ned Batchelder.

You are not storing the updated float type. You have to assign it back.

do this:

rain_amount=float(rain_amount)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM