0

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.

5
  • possible duplicate of Parse String to Float or Int Commented Dec 30, 2013 at 20:12
  • 1
    This 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 for guidance. Commented Dec 30, 2013 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. Commented Dec 30, 2013 at 20:15
  • The question is unclear. I'm assuming they did some variant of open('foo.txt').readlines(). Commented Dec 30, 2013 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. Commented Dec 30, 2013 at 20:31

3 Answers 3

3

Cast the first entry in the list to a float() value:

value = float(somelist[0])
Sign up to request clarification or add additional context in comments.

5 Comments

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
@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.
@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.
sorry for the confusion.
@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. :-)
0

This code will open a text file with a float on each line and give you a list of floats

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]

Comments

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

2 Comments

If a question is a duplicate, close it as a duplicate, don't write an answer with a link to the original question.
My apologies. New to the whole community. Will do from now on.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.