[Tutor] UTF-8 character

Dave Angel davea at ieee.org
Thu Aug 12 12:30:59 CEST 2010


Evidenca B d.o.o. wrote:
> Hi.
>
>
>
> I have a problem. I am making pa program which, makes a .txt  document, the
> problem is in my language have special character like this: č,š,ž. They don
> t appear in aschi code, but you can find them in UTF-8 code. So wen I wont
> to save with writelines a string with one of those characters in tells me
> that I have to have string or that some of the characters don't appear in
> ashi.
>
> I don't know how to solve this problem, pleas help me.
>
>
>
> THX 
>
> Karmen
>   
You don't say what version of Python you use, and you don't show either 
the code or the error traceback.  So we can only guess what your problem is.

I'll assume you're using Python 2.6 or 2.7, running on Windows, and that 
you have code like the following:  I'll also assume you've solved the 
source file encoding issues that pertain to string literals.

....
#mystrings is a list of Unicode strings with some non-ascii characters 
in one or more of them
    outfile = open(filename, "w")
    outfile.writelines(mystrings)

If you replace the open with
    outfile = codecs.open(filename,'wb','utf-8-sig')

you should correctly write the unicode as utf8 strings to the file.

DaveA



More information about the Tutor mailing list