10-24-2020, 03:12 PM
How to Read a File Line-By-Line and Store Into a List?
<div><p><strong>Summary: </strong>Use one of the following ways to read a file line by line and store into a list:</p>
<ul>
<li>Using The <code>readlines </code>And <code>strip</code> Method</li>
<li>Using <code>rstrip()</code></li>
<li>Use the <code>for </code>Loop and <code>strip()</code> method</li>
<li>Use <code>splitlines()</code></li>
<li>Use The <code>pathlib </code>Library And The<code> splitlines()</code> Method</li>
<li>Use List Comprehension</li>
</ul>
<p><strong>Problem: </strong>How to read every line of a file in Python and store each line as an element in a list?</p>
<p>In this article we are going to discuss how we can –</p>
<ul>
<li>Read a file line by line. </li>
<li>Then store it in a list.</li>
</ul>
<p>Let us have a look at an example given below that we will be referring while discussing the solutions.</p>
<p><strong><em>Given File:</em></strong><em> </em></p>
<figure class="wp-block-image size-large"><img loading="lazy" width="626" height="278" src="https://blog.finxter.com/wp-content/uploads/2020/10/image-194.png" alt="" class="wp-image-15543" srcset="https://blog.finxter.com/wp-content/uploads/2020/10/image-194.png 626w, https://blog.finxter.com/wp-content/uplo...00x133.png 300w, https://blog.finxter.com/wp-content/uplo...150x67.png 150w" sizes="(max-width: 626px) 100vw, 626px" /></figure>
<p><strong>Output:</strong></p>
<pre class="EnlighterJSRAW" data-enlighter-language="generic" data-enlighter-theme="" data-enlighter-highlight="" data-enlighter-linenumbers="" data-enlighter-lineoffset="" data-enlighter-title="" data-enlighter-group="">['Jeff Bezos', 'Bill Gates', 'Mark Zuckerberg', 'Bernard Arnault & family', 'Mukesh Ambani', 'Steve Ballmer', 'Warren Buffett', 'Larry Page', 'Elon Musk', 'Sergey Brin']</pre>
<p>In the above example we have a file by the name <strong>test.txt</strong> that stores the names of few well known personalities <img src="https://s.w.org/images/core/emoji/13.0.0/72x72/1f609.png" alt="?" class="wp-smiley" style="height: 1em; max-height: 1em;" />. Our purpose is to read each line (which in this case contains names) one by one and store them in a list.</p>
<p><strong>Note: </strong>The file taken into consideration is the same file as mentioned in the example above. Therefore the solution derived is in accordance to the same file. I have attached the file below <img src="https://s.w.org/images/core/emoji/13.0.0/72x72/1f447.png" alt="?" class="wp-smiley" style="height: 1em; max-height: 1em;" /> for your convenience. Please feel free to download it in case you want to practice with it.</p>
<div class="wp-block-file aligncenter"><a href="https://blog.finxter.com/wp-content/uploads/2020/10/test.txt">test</a><a href="https://blog.finxter.com/wp-content/uploads/2020/10/test.txt" class="wp-block-file__button" download>Download</a></div>
<p>Without further delay let us dive into the solutions.</p>
<h2>Method 1: Using The <span style="color:#ff6900" class="has-inline-color">readlines </span>And <span style="color:#ff6900" class="has-inline-color">strip </span>Methods</h2>
<ul>
<li><code><span style="color:#cf2e2e" class="has-inline-color">readlines()</span></code> <strong>is a built-in method in Python used to read a file line by line and then store each line in a list. </strong></li>
<li><strong><code><span style="color:#cf2e2e" class="has-inline-color">string.strip()</span></code>: Removes leading and trailing whitespaces including newline characters ‘\n’ and tabular characters ‘\t’.</strong></li>
</ul>
<p>We are going to use the <code>readlines() </code>method to read the file line by line while the <code>strip()</code> method is used to get rid of the new line character <code>'\n' </code>while storing the elements in the list. Let us have a look at the following program to visualize how we can solve our problem using the above mentioned methods.</p>
<pre class="EnlighterJSRAW" data-enlighter-language="generic" data-enlighter-theme="" data-enlighter-highlight="" data-enlighter-linenumbers="" data-enlighter-lineoffset="" data-enlighter-title="" data-enlighter-group="">with open('test.txt') as f: content = f.readlines()
# you may also want to remove whitespace characters like `\n` at the end of each line
li = [x.strip() for x in content]
print(li)</pre>
<p><strong>Output:</strong></p>
<pre class="EnlighterJSRAW" data-enlighter-language="generic" data-enlighter-theme="" data-enlighter-highlight="" data-enlighter-linenumbers="" data-enlighter-lineoffset="" data-enlighter-title="" data-enlighter-group="">['Jeff Bezos', 'Bill Gates', 'Mark Zuckerberg', 'Bernard Arnault & family', 'Mukesh Ambani', 'Steve Ballmer', 'Warren Buffett', 'Larry Page', 'Elon Musk', 'Sergey Brin']</pre>
<h2>Method 2: Using <span style="color:#ff6900" class="has-inline-color">line.rstrip()</span></h2>
<p><strong><code><span style="color:#cf2e2e" class="has-inline-color">string.rstrip()</span></code> is a built-in function in Python removes all whitespaces on the right of the string (trailing whitespaces).</strong> Thus, we can use it to strip or separate elements out of each line and then store them in a list using the [] notation.</p>
<p><strong>Example:</strong></p>
<pre class="EnlighterJSRAW" data-enlighter-language="generic" data-enlighter-theme="" data-enlighter-highlight="" data-enlighter-linenumbers="" data-enlighter-lineoffset="" data-enlighter-title="" data-enlighter-group="">with open('test.txt') as f: lines = [line.rstrip() for line in f]
print(lines)</pre>
<p><strong>Output:</strong></p>
<pre class="EnlighterJSRAW" data-enlighter-language="generic" data-enlighter-theme="" data-enlighter-highlight="" data-enlighter-linenumbers="" data-enlighter-lineoffset="" data-enlighter-title="" data-enlighter-group="">['Jeff Bezos', 'Bill Gates', 'Mark Zuckerberg', 'Bernard Arnault & family', 'Mukesh Ambani', 'Steve Ballmer', 'Warren Buffett', 'Larry Page', 'Elon Musk', 'Sergey Brin']</pre>
<h2>Method 3: Using the <span style="color:#ff6900" class="has-inline-color">for</span> Loop and <span style="color:#ff6900" class="has-inline-color">strip()</span> method</h2>
<p>Another approach to our problem is to use a for loop to iterate over the lines in the file one by one and then append them to a list using the <strong><code>append()</code></strong> function. The <strong><code>strip()</code></strong> function again comes into play which allows us to strip the newline character. </p>
<pre class="EnlighterJSRAW" data-enlighter-language="generic" data-enlighter-theme="" data-enlighter-highlight="" data-enlighter-linenumbers="" data-enlighter-lineoffset="" data-enlighter-title="" data-enlighter-group="">with open("test.txt") as file_in: lines = [] for line in file_in: lines.append(line.strip('\n')) print(lines)</pre>
<p><strong>Output:</strong></p>
<pre class="EnlighterJSRAW" data-enlighter-language="generic" data-enlighter-theme="" data-enlighter-highlight="" data-enlighter-linenumbers="" data-enlighter-lineoffset="" data-enlighter-title="" data-enlighter-group="">['Jeff Bezos', 'Bill Gates', 'Mark Zuckerberg', 'Bernard Arnault & family', 'Mukesh Ambani', 'Steve Ballmer', 'Warren Buffett', 'Larry Page', 'Elon Musk', 'Sergey Brin']</pre>
<h2>Method 4: Using <span style="color:#ff6900" class="has-inline-color">splitlines()</span></h2>
<p>❖ <code><span style="color:#cf2e2e" class="has-inline-color">splitlines()</span></code> is an inbuilt function in Python which is used to split a string breaking at line boundaries. </p>
<p><strong>Example:</strong></p>
<pre class="EnlighterJSRAW" data-enlighter-language="generic" data-enlighter-theme="" data-enlighter-highlight="" data-enlighter-linenumbers="" data-enlighter-lineoffset="" data-enlighter-title="" data-enlighter-group=""># Open the file for reading.
with open('test.txt', 'r') as infile: data = infile.read() # Read the contents of the file into memory. # Return a list of the lines, breaking at line boundaries.
li = data.splitlines()
print(li)</pre>
<p><strong>Output:</strong></p>
<pre class="EnlighterJSRAW" data-enlighter-language="generic" data-enlighter-theme="" data-enlighter-highlight="" data-enlighter-linenumbers="" data-enlighter-lineoffset="" data-enlighter-title="" data-enlighter-group="">['Jeff Bezos', 'Bill Gates', 'Mark Zuckerberg', 'Bernard Arnault & family', 'Mukesh Ambani', 'Steve Ballmer', 'Warren Buffett', 'Larry Page', 'Elon Musk', 'Sergey Brin']</pre>
<p> In the solution above, we’re opening the file for reading and assigning it to the variable ‘<strong><code>infile</code></strong>.’ Once the code has finished running, the file will be automatically closed. Then we use the <code>splitlines()</code> method to store it in a list by storing each line of the file as a separate element.</p>
<h2>Method 5: Using The <span style="color:#ff6900" class="has-inline-color">pathlib</span> Library And The <span style="color:#ff6900" class="has-inline-color">splitlines()</span> Method</h2>
<p>The <strong>pathlib </strong>library was introduced in Python 3.4 and has a handy method known as <code>read_text()</code> which is a nice way to read the file without having to worry about opening or closing it. The <code>splitlines</code> function turns the contents of the file to a list containing the elements of the file line by line. </p>
<p><strong>Example:</strong> </p>
<pre class="EnlighterJSRAW" data-enlighter-language="generic" data-enlighter-theme="" data-enlighter-highlight="" data-enlighter-linenumbers="" data-enlighter-lineoffset="" data-enlighter-title="" data-enlighter-group="">from pathlib import Path
p = Path('test.txt')
lines = p.read_text().splitlines()
print(lines)</pre>
<p><strong>Output:</strong></p>
<pre class="EnlighterJSRAW" data-enlighter-language="generic" data-enlighter-theme="" data-enlighter-highlight="" data-enlighter-linenumbers="" data-enlighter-lineoffset="" data-enlighter-title="" data-enlighter-group="">['Jeff Bezos', 'Bill Gates', 'Mark Zuckerberg', 'Bernard Arnault & family', 'Mukesh Ambani', 'Steve Ballmer', 'Warren Buffett', 'Larry Page', 'Elon Musk', 'Sergey Brin']</pre>
<h2>Method 6: Using <a href="https://blog.finxter.com/list-comprehension/" target="_blank" rel="noreferrer noopener">List Comprehension</a></h2>
<p><strong>List comprehension is a compact way of creating lists. The simple formula is <code>[expression + context]</code>.</strong></p>
<ul>
<li><strong>Expression: What to do with each list element?</strong></li>
<li><strong>Context: What elements to select? The context consists of an arbitrary number of <code>for</code> and <code>if</code> statements.</strong></li>
</ul>
<p><strong>The example <code>[x for x in range(3)]</code> creates the list <code>[0, 1, 2]</code>.</strong></p>
<p>If you want to learn more about list comprehensions, please have a look at our <a href="https://blog.finxter.com/list-comprehension/" target="_blank" rel="noreferrer noopener">blog tutorial here</a>. Now let us have a look at a one-line solution to our problem using list comprehension.</p>
<pre class="EnlighterJSRAW" data-enlighter-language="generic" data-enlighter-theme="" data-enlighter-highlight="" data-enlighter-linenumbers="" data-enlighter-lineoffset="" data-enlighter-title="" data-enlighter-group="">print([line.rstrip() for line in open('test.txt')])</pre>
<p><strong>output:</strong></p>
<pre class="EnlighterJSRAW" data-enlighter-language="generic" data-enlighter-theme="" data-enlighter-highlight="" data-enlighter-linenumbers="" data-enlighter-lineoffset="" data-enlighter-title="" data-enlighter-group="">['Jeff Bezos', 'Bill Gates', 'Mark Zuckerberg', 'Bernard Arnault & family', 'Mukesh Ambani', 'Steve Ballmer', 'Warren Buffett', 'Larry Page', 'Elon Musk', 'Sergey Brin']</pre>
<h2>Method 7: Reading a <span style="color:#ff6900" class="has-inline-color">csv </span>File Line By Line And Store In a List</h2>
<p>Thus far we have seen how we can read a text file line by line and store the elements in a list. Now let us discuss how we can do the same for a <strong>csv </strong>file. The approach used by us, in this case, is the <strong><a href="https://blog.finxter.com/category/pandas-library/" target="_blank" rel="noreferrer noopener">pandas </a></strong>library in Python which allows us to read the data from the <strong>csv </strong>file and store the values in an array. We can convert the array to a list using the <code>tolist()</code> method.</p>
<p>The file that we are going to mention in the example to follow looks like the one given blow.</p>
<figure class="wp-block-image size-large"><img loading="lazy" width="300" height="352" src="https://blog.finxter.com/wp-content/uploads/2020/10/image-195.png" alt="" class="wp-image-15546" srcset="https://blog.finxter.com/wp-content/uploads/2020/10/image-195.png 300w, https://blog.finxter.com/wp-content/uplo...56x300.png 256w, https://blog.finxter.com/wp-content/uplo...50x176.png 150w" sizes="(max-width: 300px) 100vw, 300px" /></figure>
<div class="wp-block-file"><a href="https://blog.finxter.com/wp-content/uploads/2020/10/test.csv">test.csv</a><a href="https://blog.finxter.com/wp-content/uploads/2020/10/test.csv" class="wp-block-file__button" download>Download</a></div>
<p>Now let us have a look at the solution to our problem in the program given below.</p>
<pre class="EnlighterJSRAW" data-enlighter-language="generic" data-enlighter-theme="" data-enlighter-highlight="" data-enlighter-linenumbers="" data-enlighter-lineoffset="" data-enlighter-title="" data-enlighter-group="">import pandas as pd
data = pd.read_csv('test.csv') # You can also add parameters such as header, sep, etc.
array = data.values
print(array.tolist())</pre>
<p><strong>Output:</strong></p>
<pre class="EnlighterJSRAW" data-enlighter-language="generic" data-enlighter-theme="" data-enlighter-highlight="" data-enlighter-linenumbers="" data-enlighter-lineoffset="" data-enlighter-title="" data-enlighter-group="">[['Bill Gates'], ['Mark Zuckerberg'], ['Bernard Arnault & family'], ['Mukesh Ambani'], ['Steve Ballmer'], ['Warren Buffett'], ['Larry Page'], ['Elon Musk'], ['Sergey Brin']]</pre>
<h2>Conclusion</h2>
<p>I hope that after reading this article you can read files line by line and then store the elements in a list such that each line represents an element of the list. Please <a href="https://blog.finxter.com/subscribe" target="_blank" rel="noreferrer noopener">subscribe </a>and <a href="https://blog.finxter.com/" target="_blank" rel="noreferrer noopener">stay tuned</a> for more interesting articles!</p>
<h2 class="wp-block-block">Where to Go From Here?</h2>
<p>Enough theory, let’s get some practice!</p>
<p>To become successful in coding, you need to get out there and solve real problems for real people. That’s how you can become a six-figure earner easily. And that’s how you polish the skills you really need in practice. After all, what’s the use of learning theory that nobody ever needs?</p>
<p><strong>Practice projects is how you sharpen your saw in coding!</strong></p>
<p>Do you want to become a code master by focusing on practical code projects that actually earn you money and solve problems for people?</p>
<p>Then become a Python freelance developer! It’s the best way of approaching the task of improving your Python skills—even if you are a complete beginner.</p>
<p>Join my free webinar <a rel="noreferrer noopener" href="https://blog.finxter.com/webinar-freelancer/" target="_blank">“How to Build Your High-Income Skill Python”</a> and watch how I grew my coding business online and how you can, too—from the comfort of your own home.</p>
<p><a href="https://blog.finxter.com/webinar-freelancer/" target="_blank" rel="noreferrer noopener">Join the free webinar now!</a></p>
<p>The post <a href="https://blog.finxter.com/how-to-read-a-file-line-by-line-and-store-into-a-list/" target="_blank" rel="noopener noreferrer">How to Read a File Line-By-Line and Store Into a List?</a> first appeared on <a href="https://blog.finxter.com/" target="_blank" rel="noopener noreferrer">Finxter</a>.</p>
</div>
https://www.sickgaming.net/blog/2020/10/...to-a-list/
<div><p><strong>Summary: </strong>Use one of the following ways to read a file line by line and store into a list:</p>
<ul>
<li>Using The <code>readlines </code>And <code>strip</code> Method</li>
<li>Using <code>rstrip()</code></li>
<li>Use the <code>for </code>Loop and <code>strip()</code> method</li>
<li>Use <code>splitlines()</code></li>
<li>Use The <code>pathlib </code>Library And The<code> splitlines()</code> Method</li>
<li>Use List Comprehension</li>
</ul>
<p><strong>Problem: </strong>How to read every line of a file in Python and store each line as an element in a list?</p>
<p>In this article we are going to discuss how we can –</p>
<ul>
<li>Read a file line by line. </li>
<li>Then store it in a list.</li>
</ul>
<p>Let us have a look at an example given below that we will be referring while discussing the solutions.</p>
<p><strong><em>Given File:</em></strong><em> </em></p>
<figure class="wp-block-image size-large"><img loading="lazy" width="626" height="278" src="https://blog.finxter.com/wp-content/uploads/2020/10/image-194.png" alt="" class="wp-image-15543" srcset="https://blog.finxter.com/wp-content/uploads/2020/10/image-194.png 626w, https://blog.finxter.com/wp-content/uplo...00x133.png 300w, https://blog.finxter.com/wp-content/uplo...150x67.png 150w" sizes="(max-width: 626px) 100vw, 626px" /></figure>
<p><strong>Output:</strong></p>
<pre class="EnlighterJSRAW" data-enlighter-language="generic" data-enlighter-theme="" data-enlighter-highlight="" data-enlighter-linenumbers="" data-enlighter-lineoffset="" data-enlighter-title="" data-enlighter-group="">['Jeff Bezos', 'Bill Gates', 'Mark Zuckerberg', 'Bernard Arnault & family', 'Mukesh Ambani', 'Steve Ballmer', 'Warren Buffett', 'Larry Page', 'Elon Musk', 'Sergey Brin']</pre>
<p>In the above example we have a file by the name <strong>test.txt</strong> that stores the names of few well known personalities <img src="https://s.w.org/images/core/emoji/13.0.0/72x72/1f609.png" alt="?" class="wp-smiley" style="height: 1em; max-height: 1em;" />. Our purpose is to read each line (which in this case contains names) one by one and store them in a list.</p>
<p><strong>Note: </strong>The file taken into consideration is the same file as mentioned in the example above. Therefore the solution derived is in accordance to the same file. I have attached the file below <img src="https://s.w.org/images/core/emoji/13.0.0/72x72/1f447.png" alt="?" class="wp-smiley" style="height: 1em; max-height: 1em;" /> for your convenience. Please feel free to download it in case you want to practice with it.</p>
<div class="wp-block-file aligncenter"><a href="https://blog.finxter.com/wp-content/uploads/2020/10/test.txt">test</a><a href="https://blog.finxter.com/wp-content/uploads/2020/10/test.txt" class="wp-block-file__button" download>Download</a></div>
<p>Without further delay let us dive into the solutions.</p>
<h2>Method 1: Using The <span style="color:#ff6900" class="has-inline-color">readlines </span>And <span style="color:#ff6900" class="has-inline-color">strip </span>Methods</h2>
<ul>
<li><code><span style="color:#cf2e2e" class="has-inline-color">readlines()</span></code> <strong>is a built-in method in Python used to read a file line by line and then store each line in a list. </strong></li>
<li><strong><code><span style="color:#cf2e2e" class="has-inline-color">string.strip()</span></code>: Removes leading and trailing whitespaces including newline characters ‘\n’ and tabular characters ‘\t’.</strong></li>
</ul>
<p>We are going to use the <code>readlines() </code>method to read the file line by line while the <code>strip()</code> method is used to get rid of the new line character <code>'\n' </code>while storing the elements in the list. Let us have a look at the following program to visualize how we can solve our problem using the above mentioned methods.</p>
<pre class="EnlighterJSRAW" data-enlighter-language="generic" data-enlighter-theme="" data-enlighter-highlight="" data-enlighter-linenumbers="" data-enlighter-lineoffset="" data-enlighter-title="" data-enlighter-group="">with open('test.txt') as f: content = f.readlines()
# you may also want to remove whitespace characters like `\n` at the end of each line
li = [x.strip() for x in content]
print(li)</pre>
<p><strong>Output:</strong></p>
<pre class="EnlighterJSRAW" data-enlighter-language="generic" data-enlighter-theme="" data-enlighter-highlight="" data-enlighter-linenumbers="" data-enlighter-lineoffset="" data-enlighter-title="" data-enlighter-group="">['Jeff Bezos', 'Bill Gates', 'Mark Zuckerberg', 'Bernard Arnault & family', 'Mukesh Ambani', 'Steve Ballmer', 'Warren Buffett', 'Larry Page', 'Elon Musk', 'Sergey Brin']</pre>
<h2>Method 2: Using <span style="color:#ff6900" class="has-inline-color">line.rstrip()</span></h2>
<p><strong><code><span style="color:#cf2e2e" class="has-inline-color">string.rstrip()</span></code> is a built-in function in Python removes all whitespaces on the right of the string (trailing whitespaces).</strong> Thus, we can use it to strip or separate elements out of each line and then store them in a list using the [] notation.</p>
<p><strong>Example:</strong></p>
<pre class="EnlighterJSRAW" data-enlighter-language="generic" data-enlighter-theme="" data-enlighter-highlight="" data-enlighter-linenumbers="" data-enlighter-lineoffset="" data-enlighter-title="" data-enlighter-group="">with open('test.txt') as f: lines = [line.rstrip() for line in f]
print(lines)</pre>
<p><strong>Output:</strong></p>
<pre class="EnlighterJSRAW" data-enlighter-language="generic" data-enlighter-theme="" data-enlighter-highlight="" data-enlighter-linenumbers="" data-enlighter-lineoffset="" data-enlighter-title="" data-enlighter-group="">['Jeff Bezos', 'Bill Gates', 'Mark Zuckerberg', 'Bernard Arnault & family', 'Mukesh Ambani', 'Steve Ballmer', 'Warren Buffett', 'Larry Page', 'Elon Musk', 'Sergey Brin']</pre>
<h2>Method 3: Using the <span style="color:#ff6900" class="has-inline-color">for</span> Loop and <span style="color:#ff6900" class="has-inline-color">strip()</span> method</h2>
<p>Another approach to our problem is to use a for loop to iterate over the lines in the file one by one and then append them to a list using the <strong><code>append()</code></strong> function. The <strong><code>strip()</code></strong> function again comes into play which allows us to strip the newline character. </p>
<pre class="EnlighterJSRAW" data-enlighter-language="generic" data-enlighter-theme="" data-enlighter-highlight="" data-enlighter-linenumbers="" data-enlighter-lineoffset="" data-enlighter-title="" data-enlighter-group="">with open("test.txt") as file_in: lines = [] for line in file_in: lines.append(line.strip('\n')) print(lines)</pre>
<p><strong>Output:</strong></p>
<pre class="EnlighterJSRAW" data-enlighter-language="generic" data-enlighter-theme="" data-enlighter-highlight="" data-enlighter-linenumbers="" data-enlighter-lineoffset="" data-enlighter-title="" data-enlighter-group="">['Jeff Bezos', 'Bill Gates', 'Mark Zuckerberg', 'Bernard Arnault & family', 'Mukesh Ambani', 'Steve Ballmer', 'Warren Buffett', 'Larry Page', 'Elon Musk', 'Sergey Brin']</pre>
<h2>Method 4: Using <span style="color:#ff6900" class="has-inline-color">splitlines()</span></h2>
<p>❖ <code><span style="color:#cf2e2e" class="has-inline-color">splitlines()</span></code> is an inbuilt function in Python which is used to split a string breaking at line boundaries. </p>
<p><strong>Example:</strong></p>
<pre class="EnlighterJSRAW" data-enlighter-language="generic" data-enlighter-theme="" data-enlighter-highlight="" data-enlighter-linenumbers="" data-enlighter-lineoffset="" data-enlighter-title="" data-enlighter-group=""># Open the file for reading.
with open('test.txt', 'r') as infile: data = infile.read() # Read the contents of the file into memory. # Return a list of the lines, breaking at line boundaries.
li = data.splitlines()
print(li)</pre>
<p><strong>Output:</strong></p>
<pre class="EnlighterJSRAW" data-enlighter-language="generic" data-enlighter-theme="" data-enlighter-highlight="" data-enlighter-linenumbers="" data-enlighter-lineoffset="" data-enlighter-title="" data-enlighter-group="">['Jeff Bezos', 'Bill Gates', 'Mark Zuckerberg', 'Bernard Arnault & family', 'Mukesh Ambani', 'Steve Ballmer', 'Warren Buffett', 'Larry Page', 'Elon Musk', 'Sergey Brin']</pre>
<p> In the solution above, we’re opening the file for reading and assigning it to the variable ‘<strong><code>infile</code></strong>.’ Once the code has finished running, the file will be automatically closed. Then we use the <code>splitlines()</code> method to store it in a list by storing each line of the file as a separate element.</p>
<h2>Method 5: Using The <span style="color:#ff6900" class="has-inline-color">pathlib</span> Library And The <span style="color:#ff6900" class="has-inline-color">splitlines()</span> Method</h2>
<p>The <strong>pathlib </strong>library was introduced in Python 3.4 and has a handy method known as <code>read_text()</code> which is a nice way to read the file without having to worry about opening or closing it. The <code>splitlines</code> function turns the contents of the file to a list containing the elements of the file line by line. </p>
<p><strong>Example:</strong> </p>
<pre class="EnlighterJSRAW" data-enlighter-language="generic" data-enlighter-theme="" data-enlighter-highlight="" data-enlighter-linenumbers="" data-enlighter-lineoffset="" data-enlighter-title="" data-enlighter-group="">from pathlib import Path
p = Path('test.txt')
lines = p.read_text().splitlines()
print(lines)</pre>
<p><strong>Output:</strong></p>
<pre class="EnlighterJSRAW" data-enlighter-language="generic" data-enlighter-theme="" data-enlighter-highlight="" data-enlighter-linenumbers="" data-enlighter-lineoffset="" data-enlighter-title="" data-enlighter-group="">['Jeff Bezos', 'Bill Gates', 'Mark Zuckerberg', 'Bernard Arnault & family', 'Mukesh Ambani', 'Steve Ballmer', 'Warren Buffett', 'Larry Page', 'Elon Musk', 'Sergey Brin']</pre>
<h2>Method 6: Using <a href="https://blog.finxter.com/list-comprehension/" target="_blank" rel="noreferrer noopener">List Comprehension</a></h2>
<p><strong>List comprehension is a compact way of creating lists. The simple formula is <code>[expression + context]</code>.</strong></p>
<ul>
<li><strong>Expression: What to do with each list element?</strong></li>
<li><strong>Context: What elements to select? The context consists of an arbitrary number of <code>for</code> and <code>if</code> statements.</strong></li>
</ul>
<p><strong>The example <code>[x for x in range(3)]</code> creates the list <code>[0, 1, 2]</code>.</strong></p>
<p>If you want to learn more about list comprehensions, please have a look at our <a href="https://blog.finxter.com/list-comprehension/" target="_blank" rel="noreferrer noopener">blog tutorial here</a>. Now let us have a look at a one-line solution to our problem using list comprehension.</p>
<pre class="EnlighterJSRAW" data-enlighter-language="generic" data-enlighter-theme="" data-enlighter-highlight="" data-enlighter-linenumbers="" data-enlighter-lineoffset="" data-enlighter-title="" data-enlighter-group="">print([line.rstrip() for line in open('test.txt')])</pre>
<p><strong>output:</strong></p>
<pre class="EnlighterJSRAW" data-enlighter-language="generic" data-enlighter-theme="" data-enlighter-highlight="" data-enlighter-linenumbers="" data-enlighter-lineoffset="" data-enlighter-title="" data-enlighter-group="">['Jeff Bezos', 'Bill Gates', 'Mark Zuckerberg', 'Bernard Arnault & family', 'Mukesh Ambani', 'Steve Ballmer', 'Warren Buffett', 'Larry Page', 'Elon Musk', 'Sergey Brin']</pre>
<h2>Method 7: Reading a <span style="color:#ff6900" class="has-inline-color">csv </span>File Line By Line And Store In a List</h2>
<p>Thus far we have seen how we can read a text file line by line and store the elements in a list. Now let us discuss how we can do the same for a <strong>csv </strong>file. The approach used by us, in this case, is the <strong><a href="https://blog.finxter.com/category/pandas-library/" target="_blank" rel="noreferrer noopener">pandas </a></strong>library in Python which allows us to read the data from the <strong>csv </strong>file and store the values in an array. We can convert the array to a list using the <code>tolist()</code> method.</p>
<p>The file that we are going to mention in the example to follow looks like the one given blow.</p>
<figure class="wp-block-image size-large"><img loading="lazy" width="300" height="352" src="https://blog.finxter.com/wp-content/uploads/2020/10/image-195.png" alt="" class="wp-image-15546" srcset="https://blog.finxter.com/wp-content/uploads/2020/10/image-195.png 300w, https://blog.finxter.com/wp-content/uplo...56x300.png 256w, https://blog.finxter.com/wp-content/uplo...50x176.png 150w" sizes="(max-width: 300px) 100vw, 300px" /></figure>
<div class="wp-block-file"><a href="https://blog.finxter.com/wp-content/uploads/2020/10/test.csv">test.csv</a><a href="https://blog.finxter.com/wp-content/uploads/2020/10/test.csv" class="wp-block-file__button" download>Download</a></div>
<p>Now let us have a look at the solution to our problem in the program given below.</p>
<pre class="EnlighterJSRAW" data-enlighter-language="generic" data-enlighter-theme="" data-enlighter-highlight="" data-enlighter-linenumbers="" data-enlighter-lineoffset="" data-enlighter-title="" data-enlighter-group="">import pandas as pd
data = pd.read_csv('test.csv') # You can also add parameters such as header, sep, etc.
array = data.values
print(array.tolist())</pre>
<p><strong>Output:</strong></p>
<pre class="EnlighterJSRAW" data-enlighter-language="generic" data-enlighter-theme="" data-enlighter-highlight="" data-enlighter-linenumbers="" data-enlighter-lineoffset="" data-enlighter-title="" data-enlighter-group="">[['Bill Gates'], ['Mark Zuckerberg'], ['Bernard Arnault & family'], ['Mukesh Ambani'], ['Steve Ballmer'], ['Warren Buffett'], ['Larry Page'], ['Elon Musk'], ['Sergey Brin']]</pre>
<h2>Conclusion</h2>
<p>I hope that after reading this article you can read files line by line and then store the elements in a list such that each line represents an element of the list. Please <a href="https://blog.finxter.com/subscribe" target="_blank" rel="noreferrer noopener">subscribe </a>and <a href="https://blog.finxter.com/" target="_blank" rel="noreferrer noopener">stay tuned</a> for more interesting articles!</p>
<h2 class="wp-block-block">Where to Go From Here?</h2>
<p>Enough theory, let’s get some practice!</p>
<p>To become successful in coding, you need to get out there and solve real problems for real people. That’s how you can become a six-figure earner easily. And that’s how you polish the skills you really need in practice. After all, what’s the use of learning theory that nobody ever needs?</p>
<p><strong>Practice projects is how you sharpen your saw in coding!</strong></p>
<p>Do you want to become a code master by focusing on practical code projects that actually earn you money and solve problems for people?</p>
<p>Then become a Python freelance developer! It’s the best way of approaching the task of improving your Python skills—even if you are a complete beginner.</p>
<p>Join my free webinar <a rel="noreferrer noopener" href="https://blog.finxter.com/webinar-freelancer/" target="_blank">“How to Build Your High-Income Skill Python”</a> and watch how I grew my coding business online and how you can, too—from the comfort of your own home.</p>
<p><a href="https://blog.finxter.com/webinar-freelancer/" target="_blank" rel="noreferrer noopener">Join the free webinar now!</a></p>
<p>The post <a href="https://blog.finxter.com/how-to-read-a-file-line-by-line-and-store-into-a-list/" target="_blank" rel="noopener noreferrer">How to Read a File Line-By-Line and Store Into a List?</a> first appeared on <a href="https://blog.finxter.com/" target="_blank" rel="noopener noreferrer">Finxter</a>.</p>
</div>
https://www.sickgaming.net/blog/2020/10/...to-a-list/