Convert Active Directory Object to string

D

Dirk Hagemann

Hi!

When I receive data from Microsoft Active Directory it is an
"ad_object" and has the type unicode. When I try to convert it to a
string I get this error:
UnicodeEncodeError: 'ascii' codec can't encode character u'\xfc' in
position 26: ordinal not in range(128)

This is caused by characters like the german ä, ö or ü.

But I (think I) need this as a string. Is there a simple solution???

regards
Dirk
 
F

Fredrik Lundh

Dirk said:
When I receive data from Microsoft Active Directory it is an
"ad_object" and has the type unicode. When I try to convert it to a
string I get this error:
UnicodeEncodeError: 'ascii' codec can't encode character u'\xfc' in
position 26: ordinal not in range(128)

This is caused by characters like the german ä, ö or ü.

But I (think I) need this as a string. Is there a simple solution???

A Unicode string is also a string.

If you want an 8-bit string, you need to decide what encoding you want to
use. Common encodings are us-ascii (which is the default if you convert from
unicode to 8-bit strings in Python), ISO-8859-1 (aka Latin-1), and UTF-8.
For example, if you want Latin-1 strings, you can use one of.

s = u.encode("iso-8859-1") # fail if some character cannot be converted
s = u.encode("iso-8859-1", "replace") # instead of failing, replace with ?
s = u.encode("iso-8859-1", "ignore") # instead of failing, leave it out

If you want an ascii string, replace "iso-8859-1" above with "ascii".

If you want to output the data to a web browser or an XML file, you can use

import cgi
s = cgi.escape(u).encode("ascii", "xmlcharrefreplace")

</F>
 
D

Dirk Hagemann

Hi Fredrik!

I think this will help me. I just have to do the same for danish
encoding and may be some further encondings...

Thanks!
Dirk
 
D

Dirk Hagemann

Ok - this really works well for german special characters, but what to
do with all the other encodings.
What I could do is to try to vonvert it to latin-1 and if it fails I
try latin-2 and so on. But is this really necessary? Isn't there may me
a module which can do this for me and returns a string?

What I want to do in the end is the following: I get some data from
Active Directory, then I create a SQL-statement including this data and
write this into the database.

regards
Dirk
 
?

=?ISO-8859-1?Q?Michael_Str=F6der?=

Dirk said:
What I want to do in the end is the following: I get some data from
Active Directory, then I create a SQL-statement including this data and
write this into the database.

Which API and protocol are you using to access Active Directory?

If you access it via LDAP (e.g. using python-ldap) you will get back
UTF-8 strings for textual attributes.

Ciao, Michael.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,968
Messages
2,570,149
Members
46,695
Latest member
StanleyDri

Latest Threads

Top