Python Forum
Finding Special Characters in a String
Thread Rating:
  • 1 Vote(s) - 3 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Finding Special Characters in a String
#1
Hello,

Ive got a script that builds a list based on data from a spreadsheet.
Each item added to the list is a string, some of which contain special characters like $, *, @, etc.

What Id like to do is remove the special characters from each item in the list.
So if the spreadsheet has 'blah $blah' Id want just 'blah blah'.

I was thinking it may be best to create a list of the characters I want to search for, then loop over each character of each list item.

If anyone could help with some pseudo code, it would be greatly appreciated.
Reply
#2
Hello,

Try this:


import re

def remove_special_characters(s=None):
    if s:
        return re.sub(r'[^a-zA-Z\d\s]', '', s)
    return None

if __name__ == '__main__':
    s = remove_special_characters('Is there any $*@#Rat^$#~ in the strawberry shortcake?')
    print(s)
results



Is there any Rat in the strawberry shortcake
Larz60+
Reply
#3
I would use the translate method of the strings. It allows for the deletion of multiple characters in one call, along with other modifications. You need a translation table, using maketrans. In Python 3.x this is a method of the string object as well, but in Python 2.x it's part of the string module.
Craig "Ichabod" O'Brien - xenomind.com
I wish you happiness.
Recommended Tutorials: BBCode, functions, classes, text adventures
Reply
#4
(Sep-30-2016, 04:17 PM)ATXpython Wrote: I was thinking it may be best to create a list of the characters I want to search for, then loop over each character of each list item.
Yes you can do it one go like this.

>>> s = 'bl"a@h $bl*ah'
>>> ''.join(c for c in s if c not in '$@"*')
'blah blah'
Reply
#5
Thank you all so much!!
Very helpful!
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Copy xml content from webpage and save to locally without special characters Nik1811 14 846 Mar-26-2024, 09:28 AM
Last Post: Nik1811
Question Special Characters read-write Prisonfeed 1 612 Sep-17-2023, 08:26 PM
Last Post: Gribouillis
  doing string split with 2 or more split characters Skaperen 22 2,487 Aug-13-2023, 01:57 AM
Last Post: Skaperen
  How do I check if the first X characters of a string are numbers? FirstBornAlbratross 6 1,521 Apr-12-2023, 10:39 AM
Last Post: jefsummers
  Finding First Digits in String giddyhead 4 1,352 Aug-17-2022, 08:12 PM
Last Post: giddyhead
Question Finding string in list item jesse68 8 1,864 Jun-30-2022, 08:27 AM
Last Post: Gribouillis
Question [SOLVED] Delete specific characters from string lines EnfantNicolas 4 2,204 Oct-21-2021, 11:28 AM
Last Post: EnfantNicolas
  Regex not finding all unicode characters tantony 3 2,279 Jul-13-2021, 09:11 PM
Last Post: tantony
  Rename Multiple files in directory to remove special characters nyawadasi 9 6,366 Feb-16-2021, 09:49 PM
Last Post: BashBedlam
  Extract continuous numeric characters from a string in Python Robotguy 2 2,632 Jan-16-2021, 12:44 AM
Last Post: snippsat

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020