7

I am trying to write a file in python, and I can't find a way to decode a byte object before writing the file, basically, I am trying to decode this bytes string:

Les \xc3\x83\xc2\xa9vad\xc3\x83\xc2\xa9s 

into this, which is the original text I'm trying to recover:

Les évadés 

I tried using the .decode('utf-8') and encode('utf-8') but nothing seems to work...

I always get Les évadés as a result... I am using python 3.4.3

Anyone can help?

4
  • 3
    Python3 uses utf8 as default encoding. From where are you getting that string? Commented Jun 9, 2015 at 18:25
  • 1
    What you're showing is utf-8 being interpreted as if it were latin-1. My guess is that Python is producing the correct output, but whatever you're printing it with is set to expect latin-1 rather than utf-8. Commented Jun 9, 2015 at 18:33
  • where did the string come from? Commented Jun 9, 2015 at 18:37
  • I'm guessing you're on Windows? Then don't use UTF-8, Windows uses different encodings by default. Commented Jun 9, 2015 at 21:08

2 Answers 2

3

And if you want a Python 3 solution:

b = b'Les \xc3\x83\xc2\xa9vad\xc3\x83\xc2\xa9s' u = b.decode('utf-8').encode('latin-1').decode('utf-8') print(u) # Les évadés 
Sign up to request clarification or add additional context in comments.

Comments

-1

What you need to do is to decode and then encode:

s = "Les \xc3\x83\xc2\xa9vad\xc3\x83\xc2\xa9s" utf = s.decode('utf-8') latin = utf.encode("latin-1","ignore") print latin 

--> Les évadés

1 Comment

s is a str, so you can't decode it on Python 3, nor is encodeing the proper final step to convert to str. And error handler of "ignore" is basically saying "throw my data on the floor, I don't care". I can see why someone down-voted. You wrote incorrect Python 2 code, that can't even run on Python 3.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.