Create an account


Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
[Tut] Python | Split String and Remove newline

#1
Python | Split String and Remove newline

<div>
<div class="kk-star-ratings kksr-auto kksr-align-left kksr-valign-top" data-payload='{&quot;align&quot;:&quot;left&quot;,&quot;id&quot;:&quot;982999&quot;,&quot;slug&quot;:&quot;default&quot;,&quot;valign&quot;:&quot;top&quot;,&quot;ignore&quot;:&quot;&quot;,&quot;reference&quot;:&quot;auto&quot;,&quot;class&quot;:&quot;&quot;,&quot;count&quot;:&quot;0&quot;,&quot;legendonly&quot;:&quot;&quot;,&quot;readonly&quot;:&quot;&quot;,&quot;score&quot;:&quot;0&quot;,&quot;best&quot;:&quot;5&quot;,&quot;gap&quot;:&quot;5&quot;,&quot;greet&quot;:&quot;Rate this post&quot;,&quot;legend&quot;:&quot;0\/5 - (0 votes)&quot;,&quot;size&quot;:&quot;24&quot;,&quot;width&quot;:&quot;0&quot;,&quot;_legend&quot;:&quot;{score}\/{best} - ({count} {votes})&quot;,&quot;font_factor&quot;:&quot;1.25&quot;}'>
<div class="kksr-stars">
<div class="kksr-stars-inactive">
<div class="kksr-star" data-star="1" style="padding-right: 5px">
<div class="kksr-icon" style="width: 24px; height: 24px;"></div>
</p></div>
<div class="kksr-star" data-star="2" style="padding-right: 5px">
<div class="kksr-icon" style="width: 24px; height: 24px;"></div>
</p></div>
<div class="kksr-star" data-star="3" style="padding-right: 5px">
<div class="kksr-icon" style="width: 24px; height: 24px;"></div>
</p></div>
<div class="kksr-star" data-star="4" style="padding-right: 5px">
<div class="kksr-icon" style="width: 24px; height: 24px;"></div>
</p></div>
<div class="kksr-star" data-star="5" style="padding-right: 5px">
<div class="kksr-icon" style="width: 24px; height: 24px;"></div>
</p></div>
</p></div>
<div class="kksr-stars-active" style="width: 0px;">
<div class="kksr-star" style="padding-right: 5px">
<div class="kksr-icon" style="width: 24px; height: 24px;"></div>
</p></div>
<div class="kksr-star" style="padding-right: 5px">
<div class="kksr-icon" style="width: 24px; height: 24px;"></div>
</p></div>
<div class="kksr-star" style="padding-right: 5px">
<div class="kksr-icon" style="width: 24px; height: 24px;"></div>
</p></div>
<div class="kksr-star" style="padding-right: 5px">
<div class="kksr-icon" style="width: 24px; height: 24px;"></div>
</p></div>
<div class="kksr-star" style="padding-right: 5px">
<div class="kksr-icon" style="width: 24px; height: 24px;"></div>
</p></div>
</p></div>
</div>
<div class="kksr-legend" style="font-size: 19.2px;"> <span class="kksr-muted">Rate this post</span> </div>
</div>
<p class="has-global-color-8-background-color has-background"><strong>Summary: </strong>The simplest way to split a string and remove the newline characters is to use a list comprehension with a if condition that eliminates the newline strings. </p>
<h3><strong>Minimal Example</strong></h3>
<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="">text = '\n-hello\n-Finxter'
words = text.split('-') # Method 1
res = [x.strip('\n') for x in words if x!='\n']
print(res) # Method 2
li = list(map(str.strip, words))
res = list(filter(bool, li))
print(res) # Method 3
import re
words = re.findall('([^-\s]+)', text)
print(words) # ['hello', 'Finxter']</pre>
<h2>Problem Formulation</h2>
<p class="has-global-color-8-background-color has-background"><strong>Problem: </strong>Say you use the split&nbsp;function to split a string on all occurrences of a certain pattern. If the pattern appears at the beginning, in between, or at the end of the string along with a newline character, the resulting split list will contain newline strings along with the required substrings. How to get rid of the newline character strings automatically?</p>
<h3><strong>Example</strong></h3>
<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="">text = '\n\tabc\n\txyz\n\tlmn\n'
words = text.split('\t') # ['\n', 'abc\n', 'xyz\n', 'lmn\n']</pre>
<p>Note the empty strings in the resulting list.</p>
<p><strong>Expected Output:</strong></p>
<pre class="wp-block-code"><code>['abc', 'xyz', 'lmn']</code></pre>
<h2>Method 1: Use a List Comprehension</h2>
<p>The trivial solution to this problem is to&nbsp;<strong>remove all newline strings</strong>&nbsp;from the resulting list using&nbsp;<a rel="noreferrer noopener" href="https://blog.finxter.com/list-comprehension/" target="_blank"><strong>list comprehension with a condition</strong></a>&nbsp;such as&nbsp;<code>[x.strip('\n') for x in words if x!='\n']</code>&nbsp;to&nbsp;<a href="https://blog.finxter.com/python-filter/">filter&nbsp;</a>out the newline strings. To be specific, the strip function in the expression allows you to get rid of the newline characters from the items, while the if condition allows you to eliminate any independently occurring newline character.</p>
<p><strong>Code:</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="">text = '\n\tabc\n\txyz\n\tlmn\n'
words = text.split('\t')
res = [x.strip('\n') for x in words if x!='\n']
print(res) # ['abc', 'xyz', 'lmn']</pre>
<h2>Method 2: Use a map and filter</h2>
<p><strong>Prerequisite</strong></p>
<ul>
<li>The&nbsp;<code>map()</code>&nbsp;function transforms one or more iterables into a new one by applying a “transformator function” to the i-th elements of each iterable. The arguments are the<em>&nbsp;transformator function object</em>&nbsp;and&nbsp;<em>one or more iterables</em>. If you pass&nbsp;<strong><em>n</em>&nbsp;iterables</strong>&nbsp;as arguments, the transformator function must be an&nbsp;<strong><em>n</em>-ary function</strong>&nbsp;taking&nbsp;<strong><em>n</em></strong>&nbsp;input arguments. The return value is an iterable map object of transformed, and possibly aggregated, elements.</li>
<li>Python’s built-in&nbsp;<code>filter()</code>&nbsp;function is used to filter out elements that pass a filtering condition. It takes two arguments:&nbsp;<code>function</code>&nbsp;and&nbsp;<code>iterable</code>. The&nbsp;<code>function</code>&nbsp;assigns a Boolean value to each element in the&nbsp;<code>iterable</code>&nbsp;to check whether the element will pass the filter or not. It returns an iterator with the elements that pass the filtering condition.</li>
</ul>
<p class="has-base-background-color has-background"><img src="https://s.w.org/images/core/emoji/14.0.0/72x72/1f30e.png" alt="?" class="wp-smiley" style="height: 1em; max-height: 1em;" /><strong>Related Read: <br />(i) <a rel="noreferrer noopener" href="https://blog.finxter.com/python-map/" target="_blank">Python map()</a></strong> <strong><br />(ii) <a rel="noreferrer noopener" href="https://blog.finxter.com/python-filter/" target="_blank">Python filter()</a></strong></p>
<p><strong>Approach: </strong>An alternative solution is to&nbsp;<strong>remove all newline strings</strong>&nbsp;from the resulting list using <code>map()</code> to first get rid of the newline characters attached to each item of the returned list and then using the&nbsp;<strong><code><a rel="noreferrer noopener" href="https://blog.finxter.com/python-filter/" target="_blank">filter()</a></code></strong>&nbsp;function such as&nbsp;<code>filter(bool, words)</code>&nbsp;to&nbsp;<a href="https://blog.finxter.com/python-filter/">filter&nbsp;</a>out any empty string&nbsp;<code>''</code>&nbsp;and other elements that evaluate to&nbsp;<code>False</code>&nbsp;such as&nbsp;<code>None</code>.</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="">text = '\n\tabc\n\txyz\n\tlmn\n'
words = text.split('\t')
li = list(map(str.strip, words))
res = list(filter(bool, li))
print(res) # ['abc', 'xyz', 'lmn']</pre>
<h2>Method 3: Use re.findall() Instead</h2>
<p>A simple and Pythonic solution is to use&nbsp;<code>re.findall(pattern, string)</code>&nbsp;with the inverse pattern used for splitting the list. If pattern A is used as a split pattern, everything that does not match pattern A can be used in the&nbsp;<code>re.findall()</code>&nbsp;function to essentially retrieve the split list.</p>
<p>Here’s the example that uses a&nbsp;<a rel="noreferrer noopener" href="https://blog.finxter.com/python-character-set-regex-tutorial/" target="_blank">negative character class</a> <code>[^\s]+</code>&nbsp;to find all characters that do not match the split pattern:</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 re text = '\n\tabc\n\txyz\n\tlmn\n'
words = re.findall('([^\s]+)', text)
print(words) # ['abc', 'xyz', 'lmn']</pre>
<p><strong>Note:</strong></p>
<p>The&nbsp;<code>re.findall(pattern, string)</code>&nbsp;method scans&nbsp;<code>string</code>&nbsp;from&nbsp;<strong>left to right</strong>, searching for all&nbsp;<strong>non-overlapping matches</strong>&nbsp;of the&nbsp;<code>pattern</code>. It returns a&nbsp;<strong>list of strings</strong>&nbsp;in the matching order when scanning the string from left to right.</p>
<figure class="wp-block-image size-full is-style-default"><img loading="lazy" decoding="async" width="768" height="432" src="https://blog.finxter.com/wp-content/uploads/2022/12/image-229.png" alt="" class="wp-image-983047" srcset="https://blog.finxter.com/wp-content/uploads/2022/12/image-229.png 768w, https://blog.finxter.com/wp-content/uplo...00x169.png 300w" sizes="(max-width: 768px) 100vw, 768px" /></figure>
<p class="has-base-background-color has-background"><img src="https://s.w.org/images/core/emoji/14.0.0/72x72/1f30e.png" alt="?" class="wp-smiley" style="height: 1em; max-height: 1em;" /><strong>Related Read: <a href="https://blog.finxter.com/python-re-findall/" target="_blank" rel="noreferrer noopener">Python re.findall() – Everything You Need to Know</a></strong></p>
<h2><strong>Exercise</strong>: <strong>Split String and Remove Empty Strings</strong></h2>
<p><strong>Problem: </strong>Say you have been given a string that has been split by the split method on all occurrences of a given pattern. The pattern appears at the end and beginning of the string. How to get rid of the empty strings automatically? </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="">s = '_hello_world_'
words = s.split('_')
print(words) # ['', 'hello', 'world', '']</pre>
<p>Note the empty strings in the resulting list.</p>
<p><strong>Expected Output:</strong></p>
<pre class="wp-block-code"><code>['hello', 'world']</code></pre>
<p><strong><img src="https://s.w.org/images/core/emoji/14.0.0/72x72/1f4a1.png" alt="?" class="wp-smiley" style="height: 1em; max-height: 1em;" /> Hint: <a rel="noreferrer noopener" href="https://blog.finxter.com/python-regex-split-without-empty-string/" target="_blank">Python Regex Split Without Empty String</a></strong></p>
<p><strong>Solution:</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="">import re s = '_hello_world_'
words = s.split('_') # Method 1: Using List Comprehension
print([x for x in words if x!='']) # Method 2: Using filter
print(list(filter(bool, words))) # Method 3: Using re.findall
print(re.findall('([^_\s]+)', s))</pre>
<h2>Conclusion</h2>
<p>Thus, we come to the end of this tutorial. We have learned how to eliminate newline characters and empty strings from a list in Python in this article. I hope it helped you and answered all your queries. Please <strong><a href="https://blog.finxter.com/subscribe/" target="_blank" rel="noreferrer noopener">subscribe</a></strong> and stay tuned for more interesting reads. </p>
<hr class="wp-block-separator has-alpha-channel-opacity" />
</div>


https://www.sickgaming.net/blog/2022/12/...e-newline/
Reply



Forum Jump:


Users browsing this thread:
1 Guest(s)

Forum software by © MyBB Theme © iAndrew 2016