Encode Python ASCII

Asked

Viewed 5,078 times

2

Hello, I am unable to correctly encode the following string in Python:

algorithm u00edtimo

I’ve tried the following alternatives:

u'algor\u00edtimo'.encode('utf-8')
'algor\u00edtimo'.decode('utf-8')
u'algor\u00edtimo'.encode('ascii')
u'algor\u00edtimo'.encode('ascii')
  • Well, off-topic, but respondendp..."algorithm" has no accent...is with "t" muted, so it’s not proparoxitone.

  • Thanks for commenting jsbueno.. plus this was just an example of a Rawler I’m doing to fetch post titles from a forum. Hug

1 answer

2


Maybe that’s what you’re looking for:

plaintext = u'algor\u00edtimo'
encodedtext = plaintext.encode('utf-8')
print (encodedtext)

DEMO

To encode a string for ascii can do:

plaintext = u'algor\u00edtimo'
decodedtext = plaintext.encode('ascii', 'ignore').decode('ascii')
print (decodedtext) # algortimo

The second function parameter encode causes likely errors to be ignored in conversion.

To decode a string you can use the function decode (or unicode):

plaintext = u'algor\u00edtimo'

encodedtext = plaintext.encode('utf-8')
decodedtext = encodedtext.decode('utf-8')

print (encodedtext)      # algorítimo
print repr(decodedtext)  # u'algor\xedtimo'

DEMO

This is applicable to Python 2, for 3 there are some differences, for more information see Unicode HOWTO. See also the coding standards.

If it is possible to specify better in which part you are having difficulties gets better to solve the problem.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.