Replacing special characters in a list in Python

Replacing special characters in a list in Python

remove special characters from list python using strip
python remove all special characters except spaces
remove character from list python
remove special characters from dataframe python
remove multiple characters from string python
remove character from string python
pyspark remove special characters
python remove non-alphanumeric characters except space

I am trying to get rid of special characters inside a list :

file_stuff
['John Smith\n', '\n', 'Gardener\n', '\n', 'Age 27\n', '\n', 'Englishman']

file_stuff_new = [x for x in file_stuff if x != '\n']
file_stuff_new = [x.replace('\n', '') for x in file_stuff_new]
file_stuff_new

['John Smith', 'Gardener', 'Age 27', 'Englishman']

This apparently works. Any other suggestions?


you could use strip(), as:

file_stuff = map(lambda s: s.strip(), file_stuff)
print(file_stuff)
// ['John Smith', '', 'Gardener', '', 'Age 27', '', 'Englishman']

use filter if you want to remove empty items from list, like

file_stuff = filter(None, map(lambda s: s.strip(), file_stuff))

remove special character in a List or String, Regular expressions can be used to create a simple tokenizer and normalizer: import re def tokens(text): "List all the word tokens in a text." return re.findall('[\w]+'​  You can replace the special characters with the desired characters as follows, import string specialCharacterText = "H#y #@w @re &*)?" inCharSet = "!@#$%^&*()[]{};:,./<>?\|`~-=_+\"" outCharSet = " " #corresponding characters in inCharSet to be replaced splCharReplaceList = string.maketrans(inCharSet, outCharSet) splCharFreeString = specialCharacterText.translate(splCharReplaceList)


You are using a raw string literal.

r'\n' is not the newline character, it's a string of length two containing the characters "\" and "n".

>>> r'\n'
'\\n'
>>> len(r'\n')
2

Otherwise, your original approach works (almost) fine.

>>> file_stuff = ['John Smith\n', '\n', 'Gardener\n', '\n', 'Age 27\n', '\n', 'Englishman']
>>> [x.replace('\n', '') for x in file_stuff]
['John Smith', '', 'Gardener', '', 'Age 27', '', 'Englishman']

We can filter out the empty strings like this:

>>> file_stuff = ['John Smith\n', '\n', 'Gardener\n', '\n', 'Age 27\n', '\n', 'Englishman']
>>> no_newline = (x.replace('\n', '') for x in file_stuff)
>>> result = [x for x in no_newline if x]
>>> result
['John Smith', 'Gardener', 'Age 27', 'Englishman']

where no_newline is a memory efficient generator that does not build an intermediary temporary list.

If you just want to strip whitespace and newline-characters from the beginning and end of your strings, consider the str.strip method.

>>> file_stuff = ['John Smith\n', '\n', 'Gardener\n', '\n', 'Age 27\n', '\n', 'Englishman']
>>> no_newline = (x.strip() for x in file_stuff)
>>> result = [x for x in no_newline if x]
>>> result
['John Smith', 'Gardener', 'Age 27', 'Englishman']

This could be shortened to

>>> result = [x.strip() for x in file_stuff if x.strip()]
>>> result
['John Smith', 'Gardener', 'Age 27', 'Englishman']

if you can deal with the inelegancy of calling str.strip twice per string.

Python, Python | Remove trailing/leading special characters from strings list. Sometimes, while working with String lists, we can have a problem in which we need to  Python Program to Replace Characters in a String Example 1. This program allows the user to enter a string, character to replace, and new character you want to replace with. Next, we used a built-in string function called replace to replace user given character with a new character.


This example is simple list-comprehension with condition :

>>> stuff = ['John Smith\n', '\n', 'Gardener\n', '\n', 'Age 27\n', '\n', 'Englishman']
>>> pure = [i.strip() for i in stuff if i.strip()]
>>> print(pure)
['John Smith', 'Gardener', 'Age 27', 'Englishman']

Python, These can be in the form of special characters for reconstructing valid character from string · Python - Remove front K characters from each string in String List  Python ''' Replace multiple characters / strings from a string ''' # Replace all the occurrences of string in list by AA in the main list otherStr = replaceMultiple(mainStr, ['s', 'l', 'a'], "AA") 1 2


You may try mapping your list to a function like replace:

file_stuff = map(lambda x: x.replace("\n", ""), file_stuff)

Python : How to replace single or multiple characters in a string , It will replace all the occurrences of strings in List toBeReplaces with newString in the main given list mainString. Let's see how to replace the  The string class has a method replace that can be used to replace substrings in a string. We can use this method to replace characters we want to remove with an empty string. For example: >>> "Hello people".replace("e", "") "Hllo popl" If you want to remove multiple characters from a string in a single line, it's better to use regular expressions.


How to Remove Special Characters from String in Java, We should remove all the special characters from the string so that we can read the string Example of removing special characters using replaceAll() method Python tutorial. Python. List of Programs. Programs. Control Systems tutorial. Hi, Is there a better way to replace/remove characters (specifically ' and" characters in my case, but it could be anything) in strings in a list, than this example to replace 'a' with 'b':


How to remove all special characters, punctuation and spaces from , To remove all special characters, punctuation and spaces from string, iterate over the string and filter out all non alpha numeric characters. To remove all special characters, punctuation and spaces from string, iterate over the string and filter out all non alpha numeric characters. For example: >>> string = "Hello $#! People Whitespace 7331" >>> ''.join(e for e in string if e.isalnum()) 'HelloPeopleWhitespace7331'.


Better way to replace/remove characters in a list of strings., Better way to replace/remove characters in a list of strings.. Python Forums on Bytes. list, than this example to replace 'a' with 'b': x = ["abbbb","123a","nnnnas"] Replace special characters by non-special characters. The generic problem faced by the programmers is removing a character from the entire string. But sometimes the requirement is way above and demands the removal of more that 1 character, but a list of such malicious characters.