![]() |
|
[Tut] Python | Split String and Remove newline - Printable Version +- Sick Gaming (https://sickgaming.net) +-- Forum: Programming (https://sickgaming.net/forum-76.html) +--- Forum: Python (https://sickgaming.net/forum-83.html) +--- Thread: [Tut] Python | Split String and Remove newline (/thread-100415.html) |
[Tut] Python | Split String and Remove newline - xSicKxBot - 12-16-2022 Python | Split String and Remove newline Rate this post Summary: 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. [b]Minimal Example[/b] Code: 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']Problem: Say you use the split 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? [b]Example[/b] Code: text = '\n\tabc\n\txyz\n\tlmn\n'
words = text.split('\t') # ['\n', 'abc\n', 'xyz\n', 'lmn\n']Note the empty strings in the resulting list. Expected Output: Code: ['abc', 'xyz', 'lmn']The trivial solution to this problem is to remove all newline strings from the resulting list using list comprehension with a condition such as Code: [x.strip('\n') for x in words if x!='\n']Code: Code: 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']Prerequisite
Related Read:(i) Python map() (ii) Python filter() Approach: An alternative solution is to remove all newline strings from the resulting list using Code: map()Code: filter()Code: filter(bool, words)Code: ''Code: FalseCode: NoneCode: 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']A simple and Pythonic solution is to use Code: re.findall(pattern, string)Code: re.findall()Here’s the example that uses a negative character class Code: [^\s]+Code: import re text = '\n\tabc\n\txyz\n\tlmn\n'
words = re.findall('([^\s]+)', text)
print(words) # ['abc', 'xyz', 'lmn']Note: The Code: re.findall(pattern, string)Code: stringCode: pattern![]() Related Read: Python re.findall() – Everything You Need to Know[b]Exercise: Split String and Remove Empty Strings[/b] Problem: 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? Code: s = '_hello_world_'
words = s.split('_')
print(words) # ['', 'hello', 'world', '']Note the empty strings in the resulting list. Expected Output: Code: ['hello', 'world'] Hint: Python Regex Split Without Empty StringSolution: Code: 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))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 subscribe and stay tuned for more interesting reads. |