Welcome to the Treehouse Community
Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.
Looking to learn something new?
Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.
Start your free trialMichael Palmer
2,337 PointsRead a file of numbers into a list (python):
I'm supposed to read a file of numbers into a list and each number has to be stored as an integer while also having only one number per line. This is what I have so far:
inFile = open('filename.txt', 'r') sortable_numbers = inFile.readlines() print(sortable_numbers)
1 Answer
Steven Parker
229,708 PointsUsing readlines will give you a list of strings.
So you'll still need to convert each one into a number of that's what you need. Perhaps something like this:
real_numbers = []
for n in sortable_numbers:
real_numbers.append(int(n))
Another idea would be to convert the file one line at a time instead of reading all at once.