Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]
Groups > comp.lang.python > #63779
| Date | 2014-01-12 08:19 -0800 |
|---|---|
| From | Albert-Jan Roskam <fomcl@yahoo.com> |
| Subject | Re: Problem writing some strings (UnicodeEncodeError) |
| Newsgroups | comp.lang.python |
| Message-ID | <mailman.5372.1389543733.18130.python-list@python.org> (permalink) |
--------------------------------------------
On Sun, 1/12/14, Paulo da Silva <p_s_d_a_s_i_l_v_a@netcabo.pt> wrote:
Subject: Problem writing some strings (UnicodeEncodeError)
To: python-list@python.org
Date: Sunday, January 12, 2014, 4:36 PM
Hi!
I am using a python3 script to produce a bash script from
lots of
filenames got using os.walk.
I have a template string for each bash command in which I
replace a
special string with the filename and then write the command
to the bash
script file.
Something like this:
shf=open(bashfilename,'w')
filenames=getfilenames() # uses os.walk
for fn in filenames:
...
cmd=templ.replace("<fn>",fn)
shf.write(cmd)
For certain filenames I got a UnicodeEncodeError exception
at
shf.write(cmd)!
I use utf-8 and have # -*- coding: utf-8 -*- in the source
.py.
How can I fix this?
Thanks for any help/comments.
======> what is the output of locale.getpreferredencoding(False)? That is the default value of the "encoding" parameter of the open function.
shf=open(bashfilename,'w', encoding='utf-8') might work, though on my Linux macine locale.getpreferredencoding(False) returns utf-8.
help(open)
...
In text mode, if encoding is not specified the encoding used is platform
dependent: locale.getpreferredencoding(False) is called to get the
current locale encoding. (For reading and writing raw bytes use binary
mode and leave encoding unspecified.)
...
Back to comp.lang.python | Previous | Next | Find similar | Unroll thread
Re: Problem writing some strings (UnicodeEncodeError) Albert-Jan Roskam <fomcl@yahoo.com> - 2014-01-12 08:19 -0800
csiph-web