1

My file is in unicode. However, for some reason, I want to change it to plain ascii while dropping any characters that are not recognized in ascii. For example, I want to change u'This is a string�' to just 'This is a string'. Following is the code I use to do so.

ascii_str = unicode_str.encode('ascii', 'ignore')

However, I still get the following annoying error.

UnicodeDecodeError: 'ascii' codec can't decode byte 0xf3 in position 0: 
  ordinal not in range(128)

How can I solve this problem? I am fine with plain ascii strings.

3
  • print repr(unicode_str) also pleas post your complete traceback Commented Nov 12, 2014 at 19:17
  • Look at my solution. I think reading the file (if there is one?) with the right encoding is the best starting point to handle that. Commented Nov 12, 2014 at 19:47
  • I can highly recommend this library for converting Unicode to ASCII: pypi.python.org/pypi/Unidecode Commented Nov 12, 2014 at 20:29

3 Answers 3

3

I assume that your unicode_str is a real unicode string.

>>> u"\xf3".encode("ascii", "ignore")
''

If not use this

>>> "\xf3".decode("ascii", "ignore").encode("ascii")

Always the best way would be, find out which encoding you deal with and than decode it. So you have an unicode string in the right format. This means start at unicode_str either to be a real unicode string or read it with the right codec. I assume that there is a file. So the very best would be:

import codecs
f = codecs.open('unicode.rst', encoding='utf-8')
for line in f:
    print repr(line)

Another desperate approach would be:

>>> import string
>>> a = "abc\xf3abc"
>>> "".join(b for b in a if b in string.printable)
'abcabc'
Sign up to request clarification or add additional context in comments.

1 Comment

This. The error message indicates an error in decoding, not encoding.
1

You need to decode it. if you have a file

with open('example.csv', 'rb') as f:
    csv = f.read().decode("utf-8")

if you wanna decode a string, you can do it this way

data.decode('UTF-8')

UPDATE You can use ord() to get code ascii of every character

d=u'This is a string'
l=[ord(s) for s in d.encode('ascii', 'ignore')]
print l

If you need to concatenate them, you can use join

print "".join(l)

2 Comments

After decoding it, how can I convert it to ascii and ignore the characters not recognized by ascii?
@MetallicPriest You can use ord() to get code ascii of every character. I've updated the post.
1

As you have a Replacement character ( a symbol found in the Unicode standard at codepoint U+FFFD in the Specials table) in your string , you need to specify that for your interpreter before decoding , with add u at the leading of your string :

>>> unicode_str=u'This is a string�'
>>> unicode_str.encode('ascii', 'ignore')
'This is a string'

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.