1

I am currently trying to make a text file with numbers into a list the text file is

1.89 1.99 2.14 2.51 5.03 3.81 1.97 2.31 2.91 3.97 2.68 2.44 

Right now I only know how to read the file. How can i make this into a list? afterwards how can I assign the list to another list? for example

jan = 1.89 feb = 1.99 

etc

Code from comments:

inFile = open('program9.txt', 'r') lineRead = inFile.readline() while lineRead != '': words = lineRead.split() annualRainfall = float(words[0]) print(format(annualRainfall, '.2f')) lineRead = inFile.readline() inFile.close() 
3
  • entire file is supposed to be a list these numbers are for a program that read the total rainfall for each of the 12 months into a list that i need to write Commented Nov 18, 2014 at 19:22
  • inFile = open('program9.txt', 'r') lineRead = inFile.readline() while lineRead != '': words = lineRead.split() annualRainfall = float(words[0]) print(format(annualRainfall, '.2f')) lineRead = inFile.readline() inFile.close() Commented Nov 18, 2014 at 19:31
  • docs.python.org/2/tutorial/…, docs.python.org/2/library/stdtypes.html#file.readlines Commented Nov 18, 2014 at 19:34

2 Answers 2

2
months = ('jan', 'feb', ...) with open('filename', 'rb') as f: my_list = [float(x) for x in f] res = dict(zip(months, my_list)) 

This will however work ONLY if there are the same number of lines than months!

Sign up to request clarification or add additional context in comments.

2 Comments

Why would you read the whole file and call splitlines on it just to get something you can use as an iterable of lines, which the file already was?
No, you can't call splitlines() on a file object; that's a method on str. Just don't call it at all. (Also, while we're at it, why are you opening the file in binary mode?)
1

A file is already an iterable of lines, so you don't have to do anything to make it into an iterable of lines.

If you want to make it specifically into a list of lines, you can do the same thing as with any other iterable: call list on it:

with open(filename) as f: lines = list(f) 

But if you want to convert this into a list of floats, it doesn't matter what kind of iterable you start with, so you might as well just use the file as-is:

with open(filename) as f: floats = [float(line) for line in f] 

(Note that float ignores trailing whitespace, so it doesn't matter whether you use a method that strips off the newlines or leaves them in place.)


From a comment:

now i just need to find out how to assign the list to another list like jan = 1.89, feb = 1.99 and so on

If you know you have exactly 12 values (and it will be an error if you don't), you can write whichever of these looks nicer to you:

jan, feb, mar, apr, may, jun, jul, aug, sep, oct, nov, dec = (float(line) for line in f) jan, feb, mar, apr, may, jun, jul, aug, sep, oct, nov, dec = map(float, f) 

However, it's often a bad idea to have 12 separate variables like this. Without knowing how you're planning to use them, it's hard to say for sure (see Keep data out of your variable names for some relevant background on making the decision yourself), but it might be better to have, say, a single dictionary, using the month names as keys:

floats = dict(zip('jan feb mar apr may jun jul aug sep oct nov dec'.split(), map(float, f)) 

Or to just leave the values in a list, and use the month names as just symbolic names for indices into that list:

>>> jan, feb, mar, apr, may, jun, jul, aug, sep, oct, nov, dec = range(12) >>> print(floats[mar]) 2.14 

That might be even nicer with an IntEnum, or maybe a namedtuple. Again, it really depends on how you plan to use the data after importing them.

8 Comments

Ah, float! This is the best solution :) I am deleting mine
is it possible to do without using "with open"?
with open creates an execution context which will ensure the file gets automatically closed when this indented block ends
this works great for my function!! thank you abarnert... now i just need to find out how to assign the list to another list like jan = 1.89, feb = 1.99 and so on...
@spuriousarbiter: Sure, it's possible to do it without a with statement, but why would you want to?
|

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.