Milano
Milano

Reputation: 18745

Python: Can't write to file - UnicodeEncodeError

This code should write some text to file. When I'm trying to write my text to console, everything works. But when I try to write the text into the file, I get UnicodeEncodeError. I know, that this is a common problem which can be solved using proper decode or encode, but I tried it and still getting the same UnicodeEncodeError. What am I doing wrong?

I've attached an example.

print "(%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s)".decode("utf-8")%(dict.get('name'),dict.get('description'),dict.get('ico'),dict.get('city'),dict.get('ulCislo'),dict.get('psc'),dict.get('weby'),dict.get('telefony'),dict.get('mobily'),dict.get('faxy'),dict.get('emaily'),dict.get('dic'),dict.get('ic_dph'),dict.get('kategorie')[0],dict.get('kategorie')[1],dict.get('kategorie')[2])

(StarBuy s.r.o.,Inzertujte s foto, auto-moto, oblečenie, reality, prácu, zvieratá, starožitnosti, dovolenky, nábytok, všetko pre deti, obuv, stroj....

with open("test.txt","wb") as f:
   f.write("(%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s)".decode("utf-8")%(dict.get('name'),dict.get('description'),dict.get('ico'),dict.get('city'),dict.get('ulCislo'),dict.get('psc'),dict.get('weby'),dict.get('telefony'),dict.get('mobily'),dict.get('faxy'),dict.get('emaily'),dict.get('dic'),dict.get('ic_dph'),dict.get('kategorie')[0],dict.get('kategorie')[1],dict.get('kategorie')[2]))

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

Where could be the problem?

Upvotes: 2

Views: 4937

Answers (5)

Ilya
Ilya

Reputation: 541

Previous answers have the right story, but as far as I can tell not the right solution. You need to set the encoding for the file object:

with open("test.txt", "w", encoding="utf-8") as f:
    f.write(stuff)

Upvotes: 0

ellockie
ellockie

Reputation: 4278

For Python 2:

  1. Declare document encoding on top of the file (if not done yet):

    # -*- coding: utf-8 -*-

  2. Replace .decode with .encode:

    with open("test.txt","wb") as f:
        f.write("(%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s)".encode("utf-8")%(dict.get('name'),dict.get('description'),dict.get('ico'),dict.get('city'),dict.get('ulCislo'),dict.get('psc'),dict.get('weby'),dict.get('telefony'),dict.get('mobily'),dict.get('faxy'),dict.get('emaily'),dict.get('dic'),dict.get('ic_dph'),dict.get('kategorie')[0],dict.get('kategorie')[1],dict.get('kategorie')[2]))
    

Upvotes: 0

jfs
jfs

Reputation: 414795

To write Unicode text to a file, you could use io.open() function:

#!/usr/bin/env python
from io import open

with open('utf8.txt', 'w', encoding='utf-8') as file:
    file.write(u'\u010d')

It is default on Python 3.

Note: you should not use the binary file mode ('b') if you want to write text.

# coding: utf8 that defines the source code encoding has nothing to do with it.

If you see sys.setdefaultencoding() outside of site.py or Python tests; assume the code is broken.

Upvotes: 6

Cosmo
Cosmo

Reputation: 940

@ned-batchelder is right. You have to declare that the system default encoding is "utf-8". The coding comment # -*- coding: utf-8 -*- doesn't do this.

To declare the system default encoding, you have to import the module sys, and call sys.setdefaultencoding('utf-8'). However, sys was previously imported by the system and its setdefaultencoding method was removed. So you have to reload it before you call the method.

So, you will need to add the following codes at the beginning:

import sys
reload(sys)
sys.setdefaultencoding('utf-8')

Upvotes: 1

SW_user2953243
SW_user2953243

Reputation: 354

You may need to explicitly declare that python use UTF-8 encoding.

The answer to this SO question explains how to do that: Declaring Encoding in Python

Upvotes: 0

Related Questions