0

I have a synopsis as follows:

synopsis = 'Eine Geschichte, wie im normalen Leben... Der als äußerst vorsichtig 
            geltende Risikoanalytiker Ruben verlässt seine Frau,...'

I am trying to write this to a file, but keep running into:

UnicodeEncodeError: 'ascii' codec can't encode character u'\xe4' in position 705: ordinal not in range(128)

Here is what I've tried:

synopsis = unicode(synopsis)
new_file.write('%s' % synopsis)

synopsis = synopsis.encode('utf-8')
new_file.write('%s' % synopsis)

In addition, I have # # -*- coding: utf-8 -*- specified at the top of my file.

Why is this occurring and how can I fix it?

4
  • you should use codecs.open() to write unicode text and use u'' for strings or 'text'.decode('utf-8') Commented Aug 10, 2012 at 23:13
  • Could you please show how this would be done in an answer? Commented Aug 10, 2012 at 23:15
  • 1
    farmdev.com/talks/unicode Commented Aug 10, 2012 at 23:16
  • 1
    yes sorry, try a = u'mytext' or 'mytextutf8'.decode('utf-8'), this gives you an unicode string, you can write an unicode string using codecs.open('dest', 'w', 'utf-8').write(myunicodestring) Commented Aug 10, 2012 at 23:16

1 Answer 1

1

How are you opening new_file?

import codecs
new_file = codecs.open('out', mode='w', encoding='utf-8')

This should allow you to write Unicode strings to the file, which will be encoded as UTF-8.

(Unless otherwise set, sys.getdefaultencoding() is 'ascii', which affects the encoding of newly-opened files.)

Sign up to request clarification or add additional context in comments.

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.