I have a file, variables.txt
, when I input a float, say 7.75
, it inputs it into the file as 7.75
. When I get the information back from the file, I get ['7.75']
. I'm assuming this is a list. How would I be able to change ['7.75']
back to a float, 7.75, so that it can be multiplied, divided, etc.
Asked
Active
Viewed 3,825 times
0

Martijn Pieters
- 1,048,767
- 296
- 4,058
- 3,343

micma
- 223
- 2
- 4
- 16
-
possible duplicate of [Parse String to Float or Int](http://stackoverflow.com/questions/379906/parse-string-to-float-or-int) – Iguananaut Dec 30 '13 at 20:12
-
1This would be a more useful question if you described _how_ you're importing the file—or, better, gave us actual (runnable, but stripped-down) sample code and data, and showed exactly where in that code you have `['7.75']` and would like to have `7.75`. See [SSCCE](http://sscce.org) for guidance. – abarnert Dec 30 '13 at 20:13
-
@Iguananaut: That question won't help someone who doesn't know how to parse `['7.75']`, and the answers are about trying to convert to float-or-int-as-appropriate rather than just to float. – abarnert Dec 30 '13 at 20:15
-
The question is unclear. I'm assuming they did some variant of `open('foo.txt').readlines()`. – Iguananaut Dec 30 '13 at 20:20
-
@Iguananaut: Yes, it's unclear, but I can't see how whatever they did could be answered by knowing who to convert a single string to a float-or-int-as-appropriate. – abarnert Dec 30 '13 at 20:31
3 Answers
3
Cast the first entry in the list to a float()
value:
value = float(somelist[0])

Martijn Pieters
- 1,048,767
- 296
- 4,058
- 3,343
-
what does somelist stand for? If you open up the file, all you see is 7.75. There isnt anything else there like variable = 7.75 – micma Dec 30 '13 at 20:13
-
@user3133761: The original variable you printed, with the list. You did not include any actual code, so I had to guess at your variable names. – Martijn Pieters Dec 30 '13 at 20:14
-
@user3133761: According to your question, you have a value `['7.75']` appearing somewhere in your code. Since you didn't tell us where that value appears, Martijn had to invent a name for a variable to hold that value, and `somelist` is a perfectly reasonable name for a variable holding a list of strings. – abarnert Dec 30 '13 at 20:14
-
-
@user3133761: Not a problem. I'd love to help you in more detail, but you haven't given us any code to work with. If this answer helped you, then that's fine too, don't forget to mark it as such. :-) – Martijn Pieters Dec 30 '13 at 20:28
0
This code will open a text file with a float on each line and give you a list of float
s
with open('floats.txt', 'r') as f:
floats = []
for each in f:
floats.append(float(each.strip()))
print(floats)
you can then access each float in the list by it's index:
float[0] #this will give you the first entry in the list
File:
7.75
1.23
4.56
Output:
[7.75, 1.23, 4.56]

kylieCatt
- 10,672
- 5
- 43
- 51
-1
In Python, how do I convert all of the items in a list to floats?
To reiterate, you want to loop through the list and convert all element to float values using the "float" function.
-
If a question is a duplicate, close it as a duplicate, don't write an answer with a link to the original question. – abarnert Dec 30 '13 at 20:16
-