Найти - Пользователи
Полная версия: [unicode] LDIFWriter и io.StringIO
Начало » Python для экспертов » [unicode] LDIFWriter и io.StringIO
1
ei-grad
$ python test123.py 
Traceback (most recent call last):
File "test123.py", line 9, in <module>
ldif_writer.unparse(dn, entry)
File "/usr/lib/python2.6/site-packages/ldif.py", line 195, in unparse
self._unparseAttrTypeandValue('dn',dn)
File "/usr/lib/python2.6/site-packages/ldif.py", line 142, in _unparseAttrTypeandValue
self._unfoldLDIFLine(': '.join([attr_type,attr_value]))
File "/usr/lib/python2.6/site-packages/ldif.py", line 107, in _unfoldLDIFLine
self._output_file.write(line)
File "/usr/lib/python2.6/io.py", line 1500, in write
s.__class__.__name__)
TypeError: can't write str to text stream

$ cat test123.py
# coding: utf-8

import io, ldif

o = io.StringIO()
ldif_writer = ldif.LDIFWriter(o)
dn='cn=Michael Stroeder,ou=Test'
entry={'objectClass':['top','person'],'cn':['Michael Stroeder'],'sn':['Stroeder']}
ldif_writer.unparse(dn, entry)
python 2.6.5

StringIO ожидает unicode, а LDIFWriter передает str. Как это правильно исправить?
ei-grad
# coding: utf-8

import io, ldif

class StrIO(io.StringIO):
def write(self, a):
return io.StringIO.write(self, unicode(a))

o = StrIO()

ldif_writer = ldif.LDIFWriter(o)
dn='cn=Michael Stroeder,ou=Test'
entry={'objectClass':['top','person'],'cn':['Michael Stroeder'],'sn':['Stroeder']}
ldif_writer.unparse(dn, entry)
ужасно же… как вы считаете?
This is a "lo-fi" version of our main content. To view the full version with more information, formatting and images, please click here.
Powered by DjangoBB