#!/usr/bin/env python
# -*- coding: utf-8 -*-

import unicodedata
text = u'Cześć'
print unicodedata.normalize('NFD', text).encode('ascii', 'ignore')
Answer from nosklo on Stack Overflow
🌐
YouTube
youtube.com › hey delphi
PYTHON : Replace special characters with ASCII equivalent - YouTube
PYTHON : Replace special characters with ASCII equivalentTo Access My Live Chat Page, On Google, Search for "hows tech developer connect"As promised, I'm goi...
Published: May 5, 2023
Views: 21
Discussions

How to replace unicode characters by ascii characters in Python (perl script given)? - Stack Overflow
def make_ascii(string): return ...).encode('ascii','ignore'); ... Find the answer to your question by asking. Ask question ... See similar questions with these tags. ... 0 Python: How to remove the majority of special unicode chars but leave accents and mutated vowels intact? 0 Replace the special characters with the near ... More on stackoverflow.com
🌐 stackoverflow.com
python - Turn special characters into ascii-like characters or someting else without losing readability - Stack Overflow
Trying to format data from ics calendar file to any outpu such as json or even python print(). Looking for good ways to replace special characters without losing readability and having ascii-like characters. Examples below. More on stackoverflow.com
🌐 stackoverflow.com
May 6, 2021
python how to convert ascii codes to original characters - Stack Overflow
Where are trying to fix it using python. We loop through our data character by character fix the corrupted values. and get an ascii # code. let say I want to replace ascii code 226 with ascii code 146, which is a special quote "’": More on stackoverflow.com
🌐 stackoverflow.com
string - Replace special characters in python - Stack Overflow
UnicodeEncodeError: 'ascii' codec can't encode characters in position 100-101: ordinal not in range(128) 2011-01-16T14:33:26.867Z+00:00 ... You should post repr() of the string that this fails for and the particular line (i.e. does it fail on the decode?). 2011-01-16T14:36:43.467Z+00:00 ... Find the answer to your question by asking. Ask question ... See similar questions with ... More on stackoverflow.com
🌐 stackoverflow.com
People also ask

How do I remove non-ASCII characters in Python?
Encode the string with ASCII and an explicit error policy such as ignore or replace, then decode it, understanding that information may be lost.
🌐
pythonpool.com
pythonpool.com › home › tutorials › remove unicode characters in python: ascii, symbols, and normalization
Remove Unicode Characters in Python: ASCII, Symbols, and Normalization
How do I remove only selected Unicode symbols?
Use str.translate() with a deletion mapping or a carefully defined regular expression rather than deleting every non-ASCII character.
🌐
pythonpool.com
pythonpool.com › home › tutorials › remove unicode characters in python: ascii, symbols, and normalization
Remove Unicode Characters in Python: ASCII, Symbols, and Normalization
Why should I avoid removing all Unicode?
Unicode includes valid letters, punctuation, and scripts; broad deletion can corrupt names, identifiers, or user content when the real requirement is narrower.
🌐
pythonpool.com
pythonpool.com › home › tutorials › remove unicode characters in python: ascii, symbols, and normalization
Remove Unicode Characters in Python: ASCII, Symbols, and Normalization
🌐
Iditect
iditect.com › faq › python › replace-special-characters-with-ascii-equivalent-in-python.html
Replace special characters with ASCII equivalent in python
To replace special characters with their ASCII equivalents in Python, you can use the unicodedata module, which provides a way to normalize and replace characters with their ASCII counterparts.
🌐
Coderanch
coderanch.com › t › 709349 › languages › Replace-special-characters
Replace special characters [Solved] (Jython/Python forum at Coderanch)
Nevertheless, I tried s = s.replace(u'ç', '') but without sucess ... I assume that when you say "doesn't work" that you actually mean that the characters in question are not removed from the text. Anyway I'm surprised that the "@" character is one of those so-called "special" characters which isn't handled correctly. That means it's not a Unicode or character set issue because that's an ordinary ASCII character. However since I'm no Jython (or Python...
🌐
py4u
py4u.org › blog › python-replace-typographical-quotes-dashes-etc-with-their-ascii-counterparts
How to Replace Typographical Quotes, Dashes, and Special Characters with ASCII Counterparts in Python (While Preserving Umlauts and Non-ASCII Text)
Replacing typographical characters with ASCII counterparts while preserving non-ASCII text like umlauts is a critical task for ensuring text compatibility across systems. By using Python’s str.translate() with a custom mapping, you can achieve this efficiently and selectively.
Find elsewhere
🌐
Scaler
scaler.com › home › topics › remove special characters from string python
Remove Special Characters From String Python - Scaler Topics
January 6, 2024 - The output for the above example will be, The maketrans() method saves the characters as their respective ASCII values. For example, the ASCII value of h and b is 104 and 98 respectively. Let us see how to use this mapping table created using the maketrans() function to remove specific special ...
🌐
Python Pool
pythonpool.com › home › tutorials › remove unicode characters in python: ascii, symbols, and normalization
Remove Unicode Characters in Python: ASCII, Symbols, and Normalization
July 13, 2026 - Replacement output is usually a temporary diagnostic format, not the final cleaned value. Encoding with ASCII and an error policy can remove characters that are not representable in ASCII.
Top answer
1 of 2
1

The data for the .ics file should not be decoded, but passed directly to .from_ical. Use res.content instead. Then Calendar generates the data decoded correctly as UTF-8 (probably part of the .ICS spec) and print can print Unicode strings correctly. For the JSON, write with utf8 encoding and ensure_ascii=False as @JosefZ recommended to see it correctly as well:

import requests
import json
from icalendar import Calendar

url = 'http://www.formula1.com/calendar/Formula_1_Official_Calendar.ics'
res = requests.get(url)
calendar = Calendar.from_ical(res.content)
events = [
    {
        'id': event['UID'].split('@')[-1].strip(),
        'startTime': event['DTSTART'].dt.strftime('%Y-%m-%dT%H:%M:%S.%f')[:-3],
        'summary': event['SUMMARY']
    } for event in calendar.walk('VEVENT') if str(event['UID']).split('@')[0].startswith('Race')]

for event in events:
    print(event['summary'])

with open('events.json', 'w', encoding='utf8') as f:
    json.dump(events, f, ensure_ascii=False, indent=2)

print Output:

FORMULA 1 GULF AIR BAHRAIN GRAND PRIX 2021 - Race
FORMULA 1 PIRELLI GRAN PREMIO DEL MADE IN ITALY E DELL'EMILIA ROMAGNA 2021 - Race
FORMULA 1 HEINEKEN GRANDE PRÉMIO DE PORTUGAL 2021 - Race
FORMULA 1 ARAMCO GRAN PREMIO DE ESPAÑA 2021 - Race
FORMULA 1 GRAND PRIX DE MONACO 2021 - Race
FORMULA 1 AZERBAIJAN GRAND PRIX 2021 - Race
FORMULA 1 HEINEKEN GRAND PRIX DU CANADA 2021 - Race
FORMULA 1 EMIRATES GRAND PRIX DE FRANCE 2021 - Race
FORMULA 1 MYWORLD GROSSER PREIS VON ÖSTERREICH 2021 - Race
FORMULA 1 PIRELLI BRITISH GRAND PRIX 2021 - Race
FORMULA 1 MAGYAR NAGYDÍJ 2021 - Race
FORMULA 1 ROLEX BELGIAN GRAND PRIX 2021 - Race
FORMULA 1 HEINEKEN DUTCH GRAND PRIX 2021 - Race
FORMULA 1 HEINEKEN GRAN PREMIO D’ITALIA 2021 - Race
FORMULA 1 VTB RUSSIAN GRAND PRIX 2021 - Race
FORMULA 1 SINGAPORE AIRLINES SINGAPORE GRAND PRIX 2021 - Race
FORMULA 1 JAPANESE GRAND PRIX 2021 - Race
FORMULA 1 ARAMCO UNITED STATES GRAND PRIX 2021 - Race
FORMULA 1 GRAN PREMIO DE LA CIUDAD DE MÉXICO 2021 - Race
FORMULA 1 HEINEKEN GRANDE PRÊMIO DE SÃO PAULO 2021 - Race
FORMULA 1 ROLEX AUSTRALIAN GRAND PRIX 2021 - Race
FORMULA 1 SAUDI ARABIAN GRAND PRIX 2021 - Race
FORMULA 1 ETIHAD AIRWAYS ABU DHABI GRAND PRIX 2021 - Race

events.json:

[
  {
    "id": "1064",
    "startTime": "2021-03-28T16:00:00.000",
    "summary": "FORMULA 1 GULF AIR BAHRAIN GRAND PRIX 2021 - Race"
  },
  {
    "id": "1065",
    "startTime": "2021-04-18T14:00:00.000",
    "summary": "FORMULA 1 PIRELLI GRAN PREMIO DEL MADE IN ITALY E DELL'EMILIA ROMAGNA 2021 - Race"
  },
  {
    "id": "1066",
    "startTime": "2021-05-02T15:00:00.000",
    "summary": "FORMULA 1 HEINEKEN GRANDE PRÉMIO DE PORTUGAL 2021 - Race"
  },
  {
    "id": "1086",
    "startTime": "2021-05-09T14:00:00.000",
    "summary": "FORMULA 1 ARAMCO GRAN PREMIO DE ESPAÑA 2021 - Race"
  },
  {
    "id": "1067",
    "startTime": "2021-05-23T14:00:00.000",
    "summary": "FORMULA 1 GRAND PRIX DE MONACO 2021 - Race"
  },
  {
    "id": "1068",
    "startTime": "2021-06-06T13:00:00.000",
    "summary": "FORMULA 1 AZERBAIJAN GRAND PRIX 2021 - Race"
  },
  {
    "id": "1069",
    "startTime": "2021-06-13T19:00:00.000",
    "summary": "FORMULA 1 HEINEKEN GRAND PRIX DU CANADA 2021 - Race"
  },
  {
    "id": "1070",
    "startTime": "2021-06-27T14:00:00.000",
    "summary": "FORMULA 1 EMIRATES GRAND PRIX DE FRANCE 2021 - Race"
  },
  {
    "id": "1071",
    "startTime": "2021-07-04T14:00:00.000",
    "summary": "FORMULA 1 MYWORLD GROSSER PREIS VON ÖSTERREICH 2021 - Race"
  },
  {
    "id": "1072",
    "startTime": "2021-07-18T15:00:00.000",
    "summary": "FORMULA 1 PIRELLI BRITISH GRAND PRIX 2021 - Race"
  },
  {
    "id": "1073",
    "startTime": "2021-08-01T14:00:00.000",
    "summary": "FORMULA 1 MAGYAR NAGYDÍJ 2021 - Race"
  },
  {
    "id": "1074",
    "startTime": "2021-08-29T14:00:00.000",
    "summary": "FORMULA 1 ROLEX BELGIAN GRAND PRIX 2021 - Race"
  },
  {
    "id": "1075",
    "startTime": "2021-09-05T14:00:00.000",
    "summary": "FORMULA 1 HEINEKEN DUTCH GRAND PRIX 2021 - Race"
  },
  {
    "id": "1076",
    "startTime": "2021-09-12T14:00:00.000",
    "summary": "FORMULA 1 HEINEKEN GRAN PREMIO D’ITALIA 2021 - Race"
  },
  {
    "id": "1077",
    "startTime": "2021-09-26T13:00:00.000",
    "summary": "FORMULA 1 VTB RUSSIAN GRAND PRIX 2021 - Race"
  },
  {
    "id": "1078",
    "startTime": "2021-10-03T13:00:00.000",
    "summary": "FORMULA 1 SINGAPORE AIRLINES SINGAPORE GRAND PRIX 2021 - Race"
  },
  {
    "id": "1079",
    "startTime": "2021-10-10T06:00:00.000",
    "summary": "FORMULA 1 JAPANESE GRAND PRIX 2021 - Race"
  },
  {
    "id": "1080",
    "startTime": "2021-10-24T20:00:00.000",
    "summary": "FORMULA 1 ARAMCO UNITED STATES GRAND PRIX 2021 - Race"
  },
  {
    "id": "1081",
    "startTime": "2021-10-31T19:00:00.000",
    "summary": "FORMULA 1 GRAN PREMIO DE LA CIUDAD DE MÉXICO 2021 - Race"
  },
  {
    "id": "1082",
    "startTime": "2021-11-07T17:00:00.000",
    "summary": "FORMULA 1 HEINEKEN GRANDE PRÊMIO DE SÃO PAULO 2021 - Race"
  },
  {
    "id": "1083",
    "startTime": "2021-11-21T06:00:00.000",
    "summary": "FORMULA 1 ROLEX AUSTRALIAN GRAND PRIX 2021 - Race"
  },
  {
    "id": "1085",
    "startTime": "2021-12-05T16:00:00.000",
    "summary": "FORMULA 1 SAUDI ARABIAN GRAND PRIX 2021 - Race"
  },
  {
    "id": "1084",
    "startTime": "2021-12-12T13:00:00.000",
    "summary": "FORMULA 1 ETIHAD AIRWAYS ABU DHABI GRAND PRIX 2021 - Race"
  }
]
2 of 2
1
with open("events.json", mode="w", encoding="utf-8") as f:
    json.dump(events, f, indent=2, ensure_ascii=False)

From json.dump docs:

If ensure_ascii is true (the default), the output is guaranteed to have all incoming non-ASCII characters escaped. If ensure_ascii is false, these characters will be output as-is.

Used encoding="utf-8" in open as the default encoding is platform dependent (whatever locale.getpreferredencoding() returns).

🌐
GitHub
gist.github.com › tushortz › 9fbde5d023c0a0204333267840b592f9
Function to replace some annoying characters · GitHub
LATIN_1_CHARS = ( ('\xe2\x80\x99', "'"), ('\xc3\xa9', 'e'), ('\xe2\x80\x90', '-'), ('\xe2\x80\x91', '-'), ('\xe2\x80\x92', '-'), ('\xe2\x80\x93', '-'), ('\xe2\x80\x94', '-'), ('\xe2\x80\x94', '-'), ('\xe2\x80\x98', "'"), ('\xe2\x80\x9b', "'"), ('\xe2\x80\x9c', '"'), ('\xe2\x80\x9c', '"'), ('\xe2\x80\x9d', '"'), ('\xe2\x80\x9e', '"'), ('\xe2\x80\x9f', '"'), ('\xe2\x80\xa6', '...'), ('\xe2\x80\xb2', "'"), ('\xe2\x80\xb3', "'"), ('\xe2\x80\xb4', "'"), ('\xe2\x80\xb5', "'"), ('\xe2\x80\xb6', "'"), ('\xe2\x80\xb7', "'"), ('\xe2\x81\xba', "+"), ('\xe2\x81\xbb', "-"), ('\xe2\x81\xbc', "="), ('\xe2\x81\xbd', "("), ('\xe2\x81\xbe', ")") ) def clean_latin1(data): try: return data.encode('utf-8') except UnicodeDecodeError: data = data.decode('iso-8859-1') for _hex, _char in LATIN_1_CHARS: data = data.replace(_hex, _char) return data.encode('utf8')
Top answer
1 of 2
7

If, s=url['title'] makes s equal to this:

In [48]: s=u'Oscar Winners Best Pictures Box Set \xc2\xa36.49'

Then the problem is

  1. in the code that defines url,
  2. or else the content from the web is mal-formed.

If Case 1, we'd need to see the code that defines url.

If Case 2, a quick-and-dirty workaround would be to encode the unicode object s with the raw-unicode-escape codec:

In [49]: print(s)
Oscar Winners Best Pictures Box Set £6.49

In [50]: print(s.encode('raw-unicode-escape'))
Oscar Winners Best Pictures Box Set £6.49

See also this SO question.


Regarding titles like s=u'Star Trek XI £3.99': Again, it would be nice fix the problem before it gets to this stage -- perhaps by looking at how url is defined. But assuming the content from the web is mal-formed, a workaround would be:

In [86]: import re

In [87]: print(re.sub(r'&#x([a-fA-F\d]+);',lambda m: unichr(int(m.group(1),base=16)),s))
Star Trek XI £3.99

A little bit of explanation:

Note that

In [51]: x=u'£'
In [53]: x.encode('utf-8')
Out[53]: '\xc2\xa3'

So the unicode object u'£', encoded with the utf-8 codec, becomes the string object '\xc2\xa3'.

Somehow, url['title'] is getting defined to be the unicode object u'\xc2\xa3'. (The u makes a big difference!)

Thus we have u'\xc2\xa3' when we desire '\xc2\xa3'. Encoding the unicode object u'\xc2\xa3' with the raw-unicode-escape codec transforms it to '\xc2\xa3'.

2 of 2
0

Edit: you have your objects already in unicode. Seems to me there is no reason to actually use enocde/decode at all.

>>> print u'Oscar Winners Best Pictures Box Set \xc2\xa36.49'.replace(u'Â','')
Oscar Winners Best Pictures Box Set £6.49

However it seems to me that something is wrong there. The unicode objects are actually not unicode; see:

>>> print 'Oscar Winners Best Pictures Box Set \xc2\xa36.49'.decode('utf8')
Oscar Winners Best Pictures Box Set £6.49

The repr() you posted should not be unicode object. That's why I was asking where are you getting the data, there is something wrong.

🌐
Python Guides
pythonguides.com › remove-unicode-characters-in-python
How to Remove Unicode Characters in Python
September 3, 2025 - I quickly realized that removing ... methods I use. ... One of the simplest ways I remove unwanted Unicode characters is by encoding the string into ASCII and then decoding it back....
🌐
Quora
quora.com › How-do-I-remove-all-special-characters-in-a-string-in-Python
How to remove all special characters in a string in Python - Quora
Answer (1 of 5): There are numerous ways to accomplish this. To remove, say, all the a’s from a string, one can use the replace() string method: [code]>>> s = 'A man, a plan, a canal: Panama' >>> s = s.replace('a', '') >>> s 'A mn, pln, cnl: Pnm' [/code]One could also “explode” the string ...
🌐
Inductive Automation
forum.inductiveautomation.com › general discussion
Replacing national characters in the string with ASCII symbols - General Discussion - Inductive Automation Forum
May 3, 2023 - We want to display logged in user on machine HMI. User name is retrieved from database and includes national characters. However, HMI accepts only ASCII symbols. How can I replace national symbols in Ignition string type tag with ASCII symbols. Something like this works in online Python interpreter but not in Ignition script console (the same string containing national characters is returned for output string) # create a dictionary with national characters and their ASCII equivalents translati...
🌐
CodeRivers
coderivers.org › blog › python-replace-accented-character-with-ascii-character
Python: Replacing Accented Characters with ASCII Characters - CodeRivers
February 22, 2026 - In many programming scenarios, especially when dealing with data that needs to be in a more standardized or restricted character set, replacing accented characters with their ASCII equivalents is a common task. Accented characters, such as é, à, or ç, are not part of the basic 7 - bit ASCII character set. Python provides several ways to perform this replacement, which can be crucial for tasks like data normalization, text processing for web applications that may have limitations in handling non-ASCII characters, or when preparing data for legacy systems that only support ASCII.
🌐
Python.org
discuss.python.org › python help
How do I replace a hex value in a string with something printable? - Python Help - Discussions on Python.org
May 22, 2024 - I have Python 3.11 on Windows 10. I’m still fairly new to Python. I have a string with data I got from a website. The website uses an extended ascii character for the minus sign and I’d like to change that to a normal p…
🌐
DCode
dcode.fr › data processing › text processing › special characters
Special Characters Remover/Detector - Online ASCII Remplacement
Tool to manage special characters: delete them, replace them, convert them to ASCII and simplify the processing of text messages without encoding issues.
🌐
GeeksforGeeks
geeksforgeeks.org › python › ascii-in-python
ascii() in Python - GeeksforGeeks
January 24, 2026 - ... Explanation: The code defines a tuple t containing the characters "Ģ", "Õ", "Õ", and "D". ascii(t) converts any non-ASCII characters in the tuple to their Unicode escape sequences.