Try list comprehension and string.strip()
:
>>> mystr = "L1\nL2\n\nL3\nL4\n \n\nL5"
>>> mystr.split('\n')
['L1', 'L2', '', 'L3', 'L4', ' ', '', 'L5']
>>> [line for line in mystr.split('\n') if line.strip() != '']
['L1', 'L2', 'L3', 'L4', 'L5']
More Related Contents:
- How to handle special cases in my Python code?
- How to convert string representation of list to a list?
- Convert a String representation of a Dictionary to a dictionary?
- How do I put a variable’s value inside a string?
- How to extract the substring between two markers?
- Remove unwanted parts from strings in a column
- What is the difference between a string and a byte string?
- Join a list of items with different types as string in Python
- Good Python modules for fuzzy string comparison? [closed]
- Python regex match OR operator
- How do I split the definition of a long string over multiple lines?
- Efficient way to add spaces between characters in a string
- Convert hex to binary
- How to extract an IP address from an HTML string?
- Regex for existence of some words whose order doesn’t matter
- How do I check for an EXACT word in a string in python
- How to test if one string is a subsequence of another? [duplicate]
- How would I get everything before a : in a string Python
- Remove duplicate chars using regex?
- How to find out if a Python object is a string?
- How do I create a multiline Python string with inline variables?
- Print without b’ prefix for bytes in Python 3
- Limiting Python input strings to certain characters and lengths
- How to check a string for a special character?
- How to split a string within a list to create key-value pairs in Python
- Python: Best Way to remove duplicate character from string
- Deleting consonants from a string in Python
- lower() vs. casefold() in string matching and converting to lowercase
- How can I convert string values from a dictionary, into int/float datatypes?
- Splitting a math expression string into tokens in Python