How do I read every line of a file in Python and store each line as an element in list?
I want to read the file line by line and each line is appended to the end of the list.
I'm guessing that you meant |
|||||||||||||||||||||
|
|
See Input and Ouput:
or with stripping the newline character:
Editor's note: This answer's original whitespace-stripping command, |
|||||||||||||||||||||
|
|
This is more explicit than necessary, but does what you want.
|
|||||||||||||||||||||
|
|
This will yield an "array" of lines from the file.
|
|||||||||||||||||||||
|
|
If you want the
If you do not want
|
|||||
|
|
You could simply do the following, as has been suggested:
Note that this approach has 2 downsides: 1) You store all the lines in memory. In the general case, this is a very bad idea. The file could be very large, and you could run out of memory. Even if it's not large, it is simply a waste of memory. 2) This does not allow processing of each line as you read them. So if you process your lines after this, it is not efficient (requires two passes rather than one). A better approach for the general case would be the following:
Where you define your process function any way you want. For example:
(The implementation of the This will work nicely for any file size and you go through your file in just 1 pass. This is typically how generic parsers will work. |
|||||||||||||||||||||
|
|
This should encapsulate the open command.
|
|||||||||
|
|
Clean and Pythonic Way of Reading the Lines of a File Into a List First and foremost, you should focus on opening your file and reading its contents in an efficient and pythonic way. Here is an example of the way I personally DO NOT prefer:
Instead, I prefer the below method of opening files for both reading and writing as it is very clean, and does not require an extra step of closing the file once you are done using it. In the statement below, we're opening the file for reading, and assigning it to the variable 'infile.' Once the code within this statement has finished running, the file will be automatically closed.
Now we need to focus on bringing this data into a Python List because they are iterable, efficient, and flexible. In your case, the desired goal is to bring each line of the text file into a separate element. To accomplish this, we will use the splitlines() method as follows:
The Final Product:
Testing Our Code:
|
|||||
|
|
As simple as it can get:
|
|||||
|
|
Another option is
This will make |
|||||||||
|
|
Here's one more option by using list comprehensions on files;
This should be more efficient way as the most of the work is done inside the Python interpreter. |
|||||||||||||
|
|
If you'd like to read a file from the command line or from stdin, you can also use the
Pass files to it like so:
Read more here: http://docs.python.org/2/library/fileinput.html |
|||
|
|
|
The simplest way to do it A simple way is to:
In one line, that would give:
|
||||
|
|
|
I'd do it like this.
|
|||||||||||||
|
Now variable out is a list (array) of what you want. You could either do:
or
you'll get the same results. |
|||
|
|
|
Just use the splitlines() functions. Here is an example.
In the output you will have the list of lines. |
|||||
|
|
An real easy way:
if you want to make it a fully fledged program, type this in:
For some reason, it doesn't read .py files properly. |
|||||
|
|
To my knowledge Python doesn't have a native array data structure. But it do supports list data structure which is much simpler to use than a Array.
|
|||||
|
|
Could also use the loadtxt command in numpy. This checks for fewer conditions than genfromtxt so it may be faster.
|
|||
|
|
|
How about this:
data is dataframe type, and use values to get ndarray, you also can get list by use |
|||
|
|
|
|||||||||||||||||
|
This should answer your question. The replace function will act as delimiter to strip the file. |
|||||
|
|
How about:
|
|||||
|
|
Declare a unix like method
And just invoke it to get the file content.
|
|||||||||
|
"textFileLines" is the array you wanted |
|||
|
|
|
|||||||||
|
Thank you for your interest in this question.
Because it has attracted low-quality or spam answers that had to be removed, posting an answer now requires 10 reputation on this site (the association bonus does not count).
Would you like to answer one of these unanswered questions instead?
for line in f:is memory efficient, fast, and leads to simple code. – Dennis Jun 9 '14 at 17:26