{"id":119766,"date":"2020-10-24T12:39:46","date_gmt":"2020-10-24T12:39:46","guid":{"rendered":"https:\/\/blog.finxter.com\/?p=15542"},"modified":"2020-10-24T12:39:46","modified_gmt":"2020-10-24T12:39:46","slug":"how-to-read-a-file-line-by-line-and-store-into-a-list","status":"publish","type":"post","link":"https:\/\/sickgaming.net\/blog\/2020\/10\/24\/how-to-read-a-file-line-by-line-and-store-into-a-list\/","title":{"rendered":"How to Read a File Line-By-Line and Store Into a List?"},"content":{"rendered":"<p><strong>Summary: <\/strong>Use one of the following ways to read a file line by line and store into a list:<\/p>\n<ul>\n<li>Using The <code>readlines <\/code>And <code>strip<\/code> Method<\/li>\n<li>Using <code>rstrip()<\/code><\/li>\n<li>Use the <code>for <\/code>Loop and <code>strip()<\/code> method<\/li>\n<li>Use <code>splitlines()<\/code><\/li>\n<li>Use The <code>pathlib <\/code>Library And The<code> splitlines()<\/code> Method<\/li>\n<li>Use List Comprehension<\/li>\n<\/ul>\n<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>\n<p>In this article we are going to discuss how we can &#8211;<\/p>\n<ul>\n<li>Read a file line by line. <\/li>\n<li>Then store it in a list.<\/li>\n<\/ul>\n<p>Let us have a look at an example given below that we will be referring while discussing the solutions.<\/p>\n<p><strong><em>Given File:<\/em><\/strong><em> <\/em><\/p>\n<figure class=\"wp-block-image size-large\"><img decoding=\"async\" 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\/uploads\/2020\/10\/image-194-300x133.png 300w, https:\/\/blog.finxter.com\/wp-content\/uploads\/2020\/10\/image-194-150x67.png 150w\" sizes=\"auto, (max-width: 626px) 100vw, 626px\" \/><\/figure>\n<p><strong>Output:<\/strong><\/p>\n<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 &amp; family', 'Mukesh Ambani', 'Steve Ballmer', 'Warren Buffett', 'Larry Page', 'Elon Musk', 'Sergey Brin']<\/pre>\n<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 decoding=\"async\" src=\"https:\/\/s.w.org\/images\/core\/emoji\/13.0.0\/72x72\/1f609.png\" alt=\"\ud83d\ude09\" 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>\n<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 decoding=\"async\" src=\"https:\/\/s.w.org\/images\/core\/emoji\/13.0.0\/72x72\/1f447.png\" alt=\"\ud83d\udc47\" class=\"wp-smiley\" style=\"height: 1em; max-height: 1em;\" \/>&nbsp; for your convenience. Please feel free to download it in case you want to practice with it.<\/p>\n<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>\n<p>Without further delay let us dive into the solutions.<\/p>\n<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>\n<ul>\n<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>\n<li><strong><code><span style=\"color:#cf2e2e\" class=\"has-inline-color\">string.strip()<\/span><\/code>: Removes leading and trailing whitespaces including newline characters \u2018\\n\u2019 and tabular characters \u2018\\t\u2019.<\/strong><\/li>\n<\/ul>\n<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>\n<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()\n# you may also want to remove whitespace characters like `\\n` at the end of each line\nli = [x.strip() for x in content]\nprint(li)<\/pre>\n<p><strong>Output:<\/strong><\/p>\n<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 &amp; family', 'Mukesh Ambani', 'Steve Ballmer', 'Warren Buffett', 'Larry Page', 'Elon Musk', 'Sergey Brin']<\/pre>\n<h2>Method 2: Using <span style=\"color:#ff6900\" class=\"has-inline-color\">line.rstrip()<\/span><\/h2>\n<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>\n<p><strong>Example:<\/strong><\/p>\n<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]\nprint(lines)<\/pre>\n<p><strong>Output:<\/strong><\/p>\n<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 &amp; family', 'Mukesh Ambani', 'Steve Ballmer', 'Warren Buffett', 'Larry Page', 'Elon Musk', 'Sergey Brin']<\/pre>\n<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>\n<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>\n<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>\n<p><strong>Output:<\/strong><\/p>\n<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 &amp; family', 'Mukesh Ambani', 'Steve Ballmer', 'Warren Buffett', 'Larry Page', 'Elon Musk', 'Sergey Brin']<\/pre>\n<h2>Method 4: Using <span style=\"color:#ff6900\" class=\"has-inline-color\">splitlines()<\/span><\/h2>\n<p>\u2756 <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>\n<p><strong>Example:<\/strong><\/p>\n<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.\nwith 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.\nli = data.splitlines()\nprint(li)<\/pre>\n<p><strong>Output:<\/strong><\/p>\n<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 &amp; family', 'Mukesh Ambani', 'Steve Ballmer', 'Warren Buffett', 'Larry Page', 'Elon Musk', 'Sergey Brin']<\/pre>\n<p>&nbsp;In the solution above, we&#8217;re opening the file for reading and assigning it to the variable &#8216;<strong><code>infile<\/code><\/strong>.&#8217; 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>\n<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>\n<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>&nbsp;function turns the contents of the file to a list containing the elements of the file line by line. <\/p>\n<p><strong>Example:<\/strong> <\/p>\n<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\np = Path('test.txt')\nlines = p.read_text().splitlines()\nprint(lines)<\/pre>\n<p><strong>Output:<\/strong><\/p>\n<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 &amp; family', 'Mukesh Ambani', 'Steve Ballmer', 'Warren Buffett', 'Larry Page', 'Elon Musk', 'Sergey Brin']<\/pre>\n<h2>Method 6: Using <a href=\"https:\/\/blog.finxter.com\/list-comprehension\/\" target=\"_blank\" rel=\"noreferrer noopener\">List Comprehension<\/a><\/h2>\n<p><strong>List comprehension is a compact way of creating lists. The simple formula is&nbsp;<code>[expression + context]<\/code>.<\/strong><\/p>\n<ul>\n<li><strong>Expression: What to do with each list element?<\/strong><\/li>\n<li><strong>Context: What elements to select? The context consists of an arbitrary number of&nbsp;<code>for<\/code>&nbsp;and&nbsp;<code>if<\/code>&nbsp;statements.<\/strong><\/li>\n<\/ul>\n<p><strong>The example&nbsp;<code>[x for x in range(3)]<\/code>&nbsp;creates the list&nbsp;<code>[0, 1, 2]<\/code>.<\/strong><\/p>\n<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>\n<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>\n<p><strong>output:<\/strong><\/p>\n<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 &amp; family', 'Mukesh Ambani', 'Steve Ballmer', 'Warren Buffett', 'Larry Page', 'Elon Musk', 'Sergey Brin']<\/pre>\n<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>\n<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>\n<p>The file that we are going to mention in the example to follow looks like the one given blow.<\/p>\n<figure class=\"wp-block-image size-large\"><img decoding=\"async\" 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\/uploads\/2020\/10\/image-195-256x300.png 256w, https:\/\/blog.finxter.com\/wp-content\/uploads\/2020\/10\/image-195-150x176.png 150w\" sizes=\"auto, (max-width: 300px) 100vw, 300px\" \/><\/figure>\n<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>\n<p>Now let us have a look at the solution to our problem in the program given below.<\/p>\n<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\ndata = pd.read_csv('test.csv') # You can also add parameters such as header, sep, etc.\narray = data.values\nprint(array.tolist())<\/pre>\n<p><strong>Output:<\/strong><\/p>\n<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 &amp; family'], ['Mukesh Ambani'], ['Steve Ballmer'], ['Warren Buffett'], ['Larry Page'], ['Elon Musk'], ['Sergey Brin']]<\/pre>\n<h2>Conclusion<\/h2>\n<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>\n<h2 class=\"wp-block-block\">Where to Go From Here?<\/h2>\n<p>Enough theory, let\u2019s get some practice!<\/p>\n<p>To become successful in coding, you need to get out there and solve real problems for real people. That\u2019s how you can become a six-figure earner easily. And that\u2019s how you polish the skills you really need in practice. After all, what\u2019s the use of learning theory that nobody ever needs?<\/p>\n<p><strong>Practice projects is how you sharpen your saw in coding!<\/strong><\/p>\n<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>\n<p>Then become a Python freelance developer! It\u2019s the best way of approaching the task of improving your Python skills\u2014even if you are a complete beginner.<\/p>\n<p>Join my free webinar <a rel=\"noreferrer noopener\" href=\"https:\/\/blog.finxter.com\/webinar-freelancer\/\" target=\"_blank\">\u201cHow to Build Your High-Income Skill Python\u201d<\/a> and watch how I grew my coding business online and how you can, too\u2014from the comfort of your own home.<\/p>\n<p><a href=\"https:\/\/blog.finxter.com\/webinar-freelancer\/\" target=\"_blank\" rel=\"noreferrer noopener\">Join the free webinar now!<\/a><\/p>\n<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>\n","protected":false},"excerpt":{"rendered":"<p>Summary: Use one of the following ways to read a file line by line and store into a list: Using The readlines And strip Method Using rstrip() Use the for Loop and strip() method Use splitlines() Use The pathlib Library And The splitlines() Method Use List Comprehension Problem: How to read every line of a [&hellip;]<\/p>\n","protected":false},"author":2,"featured_media":0,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[857],"tags":[73,468,528],"class_list":["post-119766","post","type-post","status-publish","format-standard","hentry","category-python-tut","tag-programming","tag-python","tag-tutorial"],"_links":{"self":[{"href":"https:\/\/sickgaming.net\/blog\/wp-json\/wp\/v2\/posts\/119766","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/sickgaming.net\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/sickgaming.net\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/sickgaming.net\/blog\/wp-json\/wp\/v2\/users\/2"}],"replies":[{"embeddable":true,"href":"https:\/\/sickgaming.net\/blog\/wp-json\/wp\/v2\/comments?post=119766"}],"version-history":[{"count":0,"href":"https:\/\/sickgaming.net\/blog\/wp-json\/wp\/v2\/posts\/119766\/revisions"}],"wp:attachment":[{"href":"https:\/\/sickgaming.net\/blog\/wp-json\/wp\/v2\/media?parent=119766"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/sickgaming.net\/blog\/wp-json\/wp\/v2\/categories?post=119766"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/sickgaming.net\/blog\/wp-json\/wp\/v2\/tags?post=119766"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}