{"id":9022,"date":"2023-11-26T10:00:59","date_gmt":"2023-11-26T04:30:59","guid":{"rendered":"https:\/\/tutorpython.com\/?p=9022"},"modified":"2023-11-26T00:19:39","modified_gmt":"2023-11-25T18:49:39","slug":"readlines-without-newline","status":"publish","type":"post","link":"https:\/\/tutorpython.com\/readlines-without-newline","title":{"rendered":"Readlines Without Newline in Python"},"content":{"rendered":"<div id=\"ez-toc-container\" class=\"ez-toc-v2_0_80 counter-hierarchy ez-toc-counter ez-toc-transparent ez-toc-container-direction\">\n<div class=\"ez-toc-title-container\"><p class=\"ez-toc-title\" style=\"cursor:inherit\">In This Article<\/p>\n<\/div><nav><ul class='ez-toc-list ez-toc-list-level-1 ' ><li class='ez-toc-page-1 ez-toc-heading-level-2'><a class=\"ez-toc-link ez-toc-heading-1\" href=\"https:\/\/tutorpython.com\/readlines-without-newline\/#Why_readlines_without_newline_in_Python\" >Why readlines without newline in Python?<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-2'><a class=\"ez-toc-link ez-toc-heading-2\" href=\"https:\/\/tutorpython.com\/readlines-without-newline\/#Two_ways_in_Python_to_read_lines_without_new_lines\" >Two ways in Python to read lines without new lines<\/a><ul class='ez-toc-list-level-3' ><li class='ez-toc-heading-level-3'><a class=\"ez-toc-link ez-toc-heading-3\" href=\"https:\/\/tutorpython.com\/readlines-without-newline\/#Python_Readlines_Without_Newline_Using_Python_rstrip\" >Python Readlines Without Newline Using Python\u00a0 rstrip()<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-3'><a class=\"ez-toc-link ez-toc-heading-4\" href=\"https:\/\/tutorpython.com\/readlines-without-newline\/#Python_Readlines_Without_Newline_Using_Python_splitlines\" >Python Readlines Without Newline Using Python splitlines()<\/a><\/li><\/ul><\/li><li class='ez-toc-page-1 ez-toc-heading-level-2'><a class=\"ez-toc-link ez-toc-heading-5\" href=\"https:\/\/tutorpython.com\/readlines-without-newline\/#A_practical_example_of_Python_Readlines_Without_Newline\" >A practical example of Python Readlines Without Newline<\/a><ul class='ez-toc-list-level-3' ><li class='ez-toc-heading-level-3'><a class=\"ez-toc-link ez-toc-heading-6\" href=\"https:\/\/tutorpython.com\/readlines-without-newline\/#Analyzing_Temperature_log_file_in_Python\" >Analyzing Temperature log file in\u00a0 Python<\/a><ul class='ez-toc-list-level-4' ><li class='ez-toc-heading-level-4'><a class=\"ez-toc-link ez-toc-heading-7\" href=\"https:\/\/tutorpython.com\/readlines-without-newline\/#Parsing_temperaturetxt\" >Parsing temperature.txt<\/a><\/li><\/ul><\/li><\/ul><\/li><\/ul><\/nav><\/div>\n<p>In this tutorial, we&#8217;ll learn how to readlines without newline in Python. Find two ways to use readlines without getting newlines with example code.<\/p>\n<p>Working with <a href=\"https:\/\/www.ibm.com\/docs\/en\/spss-statistics\/24.0.0?topic=files-raw-data\" target=\"_blank\" rel=\"noopener\">raw data files<\/a> is a common task in data analysis using Python. Raw data files are usually organized into lines of data, where each line is a single record or observation. However, each line is separated from the next using a <a href=\"https:\/\/en.wikipedia.org\/wiki\/Newline#:~:text=Writing%20&#039;%5Cn&#039;%20to%20a,binary%20mode%20is%20slightly%20better.\" target=\"_blank\" rel=\"noopener\">newline character<\/a>, which could interfere with our data analysis if we do not remove them.<\/p>\n<h2><span class=\"ez-toc-section\" id=\"Why_readlines_without_newline_in_Python\"><\/span>Why readlines without newline in Python?<span class=\"ez-toc-section-end\"><\/span><\/h2>\n<p>When reading data from a file using Python <strong>file.readlines<\/strong>, the <a href=\"https:\/\/tutorpython.com\/tutorial\/functions-in-python\" target=\"_blank\" rel=\"noopener\">function<\/a> returns a list of <a href=\"https:\/\/tutorpython.com\/tutorial\/strings-in-python\" target=\"_blank\" rel=\"noopener\">strings<\/a>, each ending with a newline character.<\/p>\n<p>The code example below shows this happening:<\/p>\n<pre class=\"language-python line-numbers\"><code># Open the text file people.txt for reading\r\nwith open('people.txt', 'r') as fr:\r\n    # Read the lines of data into a list\r\n    lines = fr.readlines()\r\n\r\n# Display all the lines of data\r\nprint(lines)\r\n<\/code><\/pre>\n<p>The output we get is:<\/p>\n<p><img fetchpriority=\"high\" decoding=\"async\" class=\"aligncenter size-full wp-image-9168\" src=\"https:\/\/tutorpython.com\/wp-content\/uploads\/2023\/11\/readlines_with_newlines.png\" alt=\"output of readlines containing newline characters\" width=\"1696\" height=\"92\" srcset=\"https:\/\/tutorpython.com\/wp-content\/uploads\/2023\/11\/readlines_with_newlines.png 1696w, https:\/\/tutorpython.com\/wp-content\/uploads\/2023\/11\/readlines_with_newlines-300x16.png 300w, https:\/\/tutorpython.com\/wp-content\/uploads\/2023\/11\/readlines_with_newlines-1024x56.png 1024w, https:\/\/tutorpython.com\/wp-content\/uploads\/2023\/11\/readlines_with_newlines-768x42.png 768w, https:\/\/tutorpython.com\/wp-content\/uploads\/2023\/11\/readlines_with_newlines-1536x83.png 1536w, https:\/\/tutorpython.com\/wp-content\/uploads\/2023\/11\/readlines_with_newlines-600x33.png 600w\" sizes=\"(max-width: 1696px) 100vw, 1696px\" \/><\/p>\n<p>The above printed list of lines shows that each line has a newline character <code>\\n<\/code>, appended to it. Therefore, the main question is how do we remove newlines when using <code>readlines<\/code> function?<\/p>\n<div>\n<div style=\"padding: 2px 16px;\">\n<h5><b>Note<\/b><\/h5>\n<p style=\"font-size: 12px;\">If you are not familiar with file object methods, you can read this article on <a href=\"https:\/\/tutorpython.com\/tutorial\/file-input-output-in-python\">file input output<\/a>.<\/p>\n<\/div>\n<\/div>\n<h2><span class=\"ez-toc-section\" id=\"Two_ways_in_Python_to_read_lines_without_new_lines\"><\/span>Two ways in Python to read lines without new lines<span class=\"ez-toc-section-end\"><\/span><\/h2>\n<p>There are several ways that we can use to make sure that no newlines occur at the end of each line returned from the Python <code>readlines<\/code> function.<\/p>\n<p>In the examples that follow, we will make use of the <a href=\"https:\/\/tutorpython.com\/tutorial\/file-input-output-in-python\" target=\"_blank\" rel=\"noopener\">file<\/a> <code>people.txt<\/code>. The text file contains the following information about people:<\/p>\n<p><img decoding=\"async\" class=\"aligncenter size-full wp-image-9169\" src=\"https:\/\/tutorpython.com\/wp-content\/uploads\/2023\/11\/contents_of_people_txt.png\" alt=\"contents of people.txt file\" width=\"1712\" height=\"128\" srcset=\"https:\/\/tutorpython.com\/wp-content\/uploads\/2023\/11\/contents_of_people_txt.png 1712w, https:\/\/tutorpython.com\/wp-content\/uploads\/2023\/11\/contents_of_people_txt-300x22.png 300w, https:\/\/tutorpython.com\/wp-content\/uploads\/2023\/11\/contents_of_people_txt-1024x77.png 1024w, https:\/\/tutorpython.com\/wp-content\/uploads\/2023\/11\/contents_of_people_txt-768x57.png 768w, https:\/\/tutorpython.com\/wp-content\/uploads\/2023\/11\/contents_of_people_txt-1536x115.png 1536w, https:\/\/tutorpython.com\/wp-content\/uploads\/2023\/11\/contents_of_people_txt-600x45.png 600w\" sizes=\"(max-width: 1712px) 100vw, 1712px\" \/><\/p>\n<p>Since <code>people.txt<\/code> has several lines of data, we can use it to demonstrate how to remove new lines from each line.<\/p>\n<h3><span class=\"ez-toc-section\" id=\"Python_Readlines_Without_Newline_Using_Python_rstrip\"><\/span>Python Readlines Without Newline Using Python\u00a0 <code>rstrip()<\/code><span class=\"ez-toc-section-end\"><\/span><\/h3>\n<p><code>rstrip<\/code> is a string method that removes all whitespaces at the end of a string instance. Whitespace includes tabs (<code>\\t<\/code>), newlines (<code>\\n<\/code>), and spaces. For example, the code below displays the string <code>Hello<\/code> without the newline character at the end:<\/p>\n<pre class=\"language-python line-numbers\"><code>print(\"Hello\\n\".rstrip('\\n'))\"    # Outputs: Hello\r\n<\/code><\/pre>\n<p>To get a list of strings without newlines, we have to call <code>rstrip<\/code> on each element of the list returned by <code>readlines<\/code>. Study the following code to see how we did it:<\/p>\n<pre class=\"language-python line-numbers\"><code># Open the text file people.txt for reading\r\nwith open('people.txt', 'r') as fr:\r\n    # Read the lines of data into a list\r\n    lines = fr.readlines()\r\n\r\n    # Use method rstrip in a list comprehension to \r\n    # clean up the lines of data\r\n    lines = [line.rstrip('\\n') for line in lines]\r\n\r\n# Display all the lines of data\r\nprint(lines)\r\n<\/code><\/pre>\n<p>The code segment produces an output that contains a list of strings cleaned up of the newline characters. It looks like what follows:<\/p>\n<p><img decoding=\"async\" class=\"aligncenter size-full wp-image-9172\" src=\"https:\/\/tutorpython.com\/wp-content\/uploads\/2023\/11\/readlines_no_newlines_rstrip.png\" alt=\"Using rstrip method to readlines without newlines\" width=\"1720\" height=\"124\" srcset=\"https:\/\/tutorpython.com\/wp-content\/uploads\/2023\/11\/readlines_no_newlines_rstrip.png 1720w, https:\/\/tutorpython.com\/wp-content\/uploads\/2023\/11\/readlines_no_newlines_rstrip-300x22.png 300w, https:\/\/tutorpython.com\/wp-content\/uploads\/2023\/11\/readlines_no_newlines_rstrip-1024x74.png 1024w, https:\/\/tutorpython.com\/wp-content\/uploads\/2023\/11\/readlines_no_newlines_rstrip-768x55.png 768w, https:\/\/tutorpython.com\/wp-content\/uploads\/2023\/11\/readlines_no_newlines_rstrip-1536x111.png 1536w, https:\/\/tutorpython.com\/wp-content\/uploads\/2023\/11\/readlines_no_newlines_rstrip-600x43.png 600w\" sizes=\"(max-width: 1720px) 100vw, 1720px\" \/><\/p>\n<h3><span class=\"ez-toc-section\" id=\"Python_Readlines_Without_Newline_Using_Python_splitlines\"><\/span>Python Readlines Without Newline Using Python <code>splitlines()<\/code><span class=\"ez-toc-section-end\"><\/span><\/h3>\n<p>A file object also allows us to make use of <code>read<\/code> method to read the data in a file.<\/p>\n<p>Unlike <code>readlines<\/code>, the <code>read<\/code> method reads the whole file as a single string. Each newline in the returned string acts as the line delimiter within the file.<\/p>\n<p>In order to separate each line, we make use of the <code>splitlines<\/code> string method:<\/p>\n<pre class=\"language-python line-numbers\"><code># Open the text file people.txt for reading\r\nwith open('people.txt', 'r') as fr:\r\n    # Read the lines of data as a single string\r\n    lines = fr.read()\r\n\r\n    # Use method splitlines to split the lines \r\n    # of data at the newline delimiters\r\n    lines = lines.splitlines()\r\n\r\n# Display all the lines of data\r\nprint(lines)\r\n<\/code><\/pre>\n<p>Our code snippet produces similar output to the previous example:<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter size-full wp-image-9174\" src=\"https:\/\/tutorpython.com\/wp-content\/uploads\/2023\/11\/readlines_no_newlines_splitlines.png\" alt=\"readlines without newlines using splitlines method\" width=\"1701\" height=\"136\" srcset=\"https:\/\/tutorpython.com\/wp-content\/uploads\/2023\/11\/readlines_no_newlines_splitlines.png 1701w, https:\/\/tutorpython.com\/wp-content\/uploads\/2023\/11\/readlines_no_newlines_splitlines-300x24.png 300w, https:\/\/tutorpython.com\/wp-content\/uploads\/2023\/11\/readlines_no_newlines_splitlines-1024x82.png 1024w, https:\/\/tutorpython.com\/wp-content\/uploads\/2023\/11\/readlines_no_newlines_splitlines-768x61.png 768w, https:\/\/tutorpython.com\/wp-content\/uploads\/2023\/11\/readlines_no_newlines_splitlines-1536x123.png 1536w, https:\/\/tutorpython.com\/wp-content\/uploads\/2023\/11\/readlines_no_newlines_splitlines-600x48.png 600w\" sizes=\"(max-width: 1701px) 100vw, 1701px\" \/><\/p>\n<p>The method <code>splitlines<\/code> &#8220;splits&#8221; the string of data (that is returned from our call to the <code>fr.read<\/code> method) into a collection of strings.<\/p>\n<p>It looks for newline characters and makes the split at those locations where the <code>\\n<\/code> character is found.<\/p>\n<p>We now want to put our newfound skill to practical use by applying it to a data analysis task.<\/p>\n<h2><span class=\"ez-toc-section\" id=\"A_practical_example_of_Python_Readlines_Without_Newline\"><\/span>A practical example of Python Readlines Without Newline<span class=\"ez-toc-section-end\"><\/span><\/h2>\n<h3><span class=\"ez-toc-section\" id=\"Analyzing_Temperature_log_file_in_Python\"><\/span>Analyzing Temperature log file in\u00a0 Python<span class=\"ez-toc-section-end\"><\/span><\/h3>\n<p>Let&#8217;s consider a real data analysis scenario where you&#8217;re reading and processing data from a daily temperature log file (<code>temperature.txt<\/code>).<\/p>\n<p>In this scenario, our log file contains daily temperature records for the month of January for a particular location.<\/p>\n<p>All temperature readings are in <span class=\"katex-eq\" data-katex-display=\"false\">\\degree C<\/span>.<\/p>\n<p><strong>Our goal is to perform data analysis on this temperature data to gain insights or make weather-related decisions<\/strong>.<\/p>\n<p>Our log file, <code>temperature.txt<\/code>, contains entries in the following format: <code>YYYY-MM-DD: TEMPERATURE<\/code>.<\/p>\n<p>Each line represents the temperature recorded for a specific date.<\/p>\n<p>Part of the file looks like the following:<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter size-full wp-image-9177\" src=\"https:\/\/tutorpython.com\/wp-content\/uploads\/2023\/11\/temperature_log_file.png\" alt=\"contents of the temperature.txt log file\" width=\"1687\" height=\"216\" srcset=\"https:\/\/tutorpython.com\/wp-content\/uploads\/2023\/11\/temperature_log_file.png 1687w, https:\/\/tutorpython.com\/wp-content\/uploads\/2023\/11\/temperature_log_file-300x38.png 300w, https:\/\/tutorpython.com\/wp-content\/uploads\/2023\/11\/temperature_log_file-1024x131.png 1024w, https:\/\/tutorpython.com\/wp-content\/uploads\/2023\/11\/temperature_log_file-768x98.png 768w, https:\/\/tutorpython.com\/wp-content\/uploads\/2023\/11\/temperature_log_file-1536x197.png 1536w, https:\/\/tutorpython.com\/wp-content\/uploads\/2023\/11\/temperature_log_file-600x77.png 600w\" sizes=\"(max-width: 1687px) 100vw, 1687px\" \/><\/p>\n<p>Here&#8217;s the complete Python code:<\/p>\n<pre class=\"language-python line-numbers\"><code>from collections import namedtuple\r\n\r\ndef wrangle(temperature_file):\r\n    with open(temperature_file, 'r') as fr:\r\n        # Read the lines of data into a list\r\n        lines = fr.readlines()\r\n        \r\n        # clean up the newlines in the lines of data\r\n        lines = [line.rstrip('\\n') for line in lines]\r\n\r\n        # Split each line at the \":\" character\r\n        lines = [line.split(':') for line in lines]\r\n\r\n        # Each sublist has a list of lists. Create a \r\n        # named tuple template\/class to represent each item\r\n        LogInfo = namedtuple('LogInfo', ['date', 'temperature'])\r\n\r\n        # Create instances of the LogInfo tuple\r\n        data = [\r\n            LogInfo(datestr, float(temperature_str))\r\n            for datestr, temperature_str in lines\r\n        ]\r\n\r\n    return data\r\n\r\ndef temperature_stats(data):\r\n    temp_values = [info.temperature for info in data]\r\n\r\n    # Get the number of days in the log file\r\n    num_days = len(data)\r\n\r\n    # Calculate the average temerature\r\n    sum_temp = sum(temp_values)\r\n    temp_avg = sum_temp\/num_days\r\n    \r\n    # Calculate the temerature range \r\n    min_temp = min(temp_values)\r\n    max_temp = max(temp_values)\r\n    temp_range = max_temp - min_temp\r\n\r\n    # Consruct the string to return\r\n    data_stats_str = \"Temperature log stats\\n\"         # The header\r\n    data_stats_str += f\"{'-'*len(data_stats_str)}\\n\"    # Underline the header\r\n    data_stats_str += f\"Total Days: {num_days}\\n\"\r\n    data_stats_str += f\"Average Temperature: {round(temp_avg, 1)}\\n\"\r\n    data_stats_str += f\"Temperature Range: {temp_range}\\n\"\r\n    \r\n    return data_stats_str\r\n\r\ndef main():\r\n    data = wrangle('temperature.txt')   # Parse the log file\r\n    print(temperature_stats(data))      # Display the stats\r\n\r\n# Run the main function\r\nmain()\r\n<\/code><\/pre>\n<p>The previous script defines a function that parses the data found in the temperature log file and another that computes some statistics about it.<\/p>\n<div>\n<div style=\"padding: 2px 16px;\">\n<h5><b>Note<\/b><\/h5>\n<p style=\"font-size: 12px;\">The article <a href=\"https:\/\/tutorpython.com\/tutorial\/context-manager-in-python\">Context manager in Python<\/a> goes into more details about using context managers.<\/p>\n<\/div>\n<\/div>\n<h4><span class=\"ez-toc-section\" id=\"Parsing_temperaturetxt\"><\/span>Parsing <code>temperature.txt<\/code><span class=\"ez-toc-section-end\"><\/span><\/h4>\n<p>The <code>wrangle<\/code> function is the part of our script that removes the Python newlines when reading the data from <code>temerature.txt<\/code>.<\/p>\n<p>Since a newline delimits each line in the file, our<code>wrangle<\/code> function implements one of the techniques we discussed in the previous sections to remove the newline characters.<\/p>\n<p><code>wrangle<\/code> parses the contents of the<code>temperature.txt<\/code> file into a list of<code>LogInfo<\/code> named tuples, which makes it easy to access the data we need later.<\/p>\n<div>\n<div style=\"padding: 2px 16px;\">\n<h5><b>Note<\/b><\/h5>\n<p style=\"font-size: 12px;\">We used a named tuple as a simplified way to create object instances. If named tuples are new to you, check out our <a href=\"https:\/\/tutorpython.com\/tutorial\/named-tuples-python\">tutorial on named tuples<\/a>.<\/p>\n<\/div>\n<\/div>\n<p>In conclusion, before we analyze our data, we can make use of the new skills learned in this Python tutorial to read lines without new lines. This will eliminate some bugs that would have resulted from leaving the newlines intact.<\/p>\n<p>Also, if you liked this tutorial, check out our docs for more <a href=\"https:\/\/tutorpython.com\/blog\" target=\"_blank\" rel=\"noopener\">how-to topics on Python<\/a>. You might also like a <a href=\"https:\/\/tutorpython.com\/\" target=\"_blank\" rel=\"noopener\">tutor for Python<\/a> programming. Happy exploring!<\/p>\n","protected":false},"excerpt":{"rendered":"<p>In this tutorial, we&#8217;ll learn how to readlines without newline in Python&#8230;.<\/p>\n","protected":false},"author":81,"featured_media":0,"comment_status":"closed","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"_acf_changed":false,"footnotes":""},"categories":[28],"tags":[],"class_list":["post-9022","post","type-post","status-publish","format-standard","hentry","category-tutorial"],"acf":[],"_links":{"self":[{"href":"https:\/\/tutorpython.com\/wp-json\/wp\/v2\/posts\/9022","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/tutorpython.com\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/tutorpython.com\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/tutorpython.com\/wp-json\/wp\/v2\/users\/81"}],"replies":[{"embeddable":true,"href":"https:\/\/tutorpython.com\/wp-json\/wp\/v2\/comments?post=9022"}],"version-history":[{"count":27,"href":"https:\/\/tutorpython.com\/wp-json\/wp\/v2\/posts\/9022\/revisions"}],"predecessor-version":[{"id":9339,"href":"https:\/\/tutorpython.com\/wp-json\/wp\/v2\/posts\/9022\/revisions\/9339"}],"wp:attachment":[{"href":"https:\/\/tutorpython.com\/wp-json\/wp\/v2\/media?parent=9022"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/tutorpython.com\/wp-json\/wp\/v2\/categories?post=9022"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/tutorpython.com\/wp-json\/wp\/v2\/tags?post=9022"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}