| | |
problem writing to a text file.
Please support our Python advertiser: Programming Forums - DaniWeb Sister Site
![]() |
•
•
Join Date: Oct 2009
Posts: 1
Reputation:
Solved Threads: 0
description:
Reading data from active directory. some names have the French accented E. when I write to screen, no problems. when I write to a .txt file the program errors out with...
Traceback (most recent call last):
File "ad-listing.py", line 20, in <module>
print >> OutFile, person.displayname,"~",person.title,\
UnicodeEncodeError: 'ascii' codec can't encode character u'\xe9' in position 9:
ordinal not in range(128)
when I try something fancy like...
print >> OutFile,(person.displayname,'utf8').....
the output of that field is...
(u'Keith Brown', 'utf8')
(u'FTP TELUS', 'utf8')
(u'FTP AT&T', 'utf8')
(u'FTP EPCOR', 'utf8')
(u'FTP SHAW', 'utf8')
WHen I try to chop off the "(u'" at the start and "', 'utf8')" at the end all the data disappears leaving the brackets..
Is the any function that can do a simple straight conversion to text so that I can print the data to a file with out massaging the heck out of the data?
Python 2.6, Windows XP Pro, Active directory
I'm trying this in Python to try to introduce an Open Source programing language where I work.
Reading data from active directory. some names have the French accented E. when I write to screen, no problems. when I write to a .txt file the program errors out with...
Traceback (most recent call last):
File "ad-listing.py", line 20, in <module>
print >> OutFile, person.displayname,"~",person.title,\
UnicodeEncodeError: 'ascii' codec can't encode character u'\xe9' in position 9:
ordinal not in range(128)
when I try something fancy like...
print >> OutFile,(person.displayname,'utf8').....
the output of that field is...
(u'Keith Brown', 'utf8')
(u'FTP TELUS', 'utf8')
(u'FTP AT&T', 'utf8')
(u'FTP EPCOR', 'utf8')
(u'FTP SHAW', 'utf8')
WHen I try to chop off the "(u'" at the start and "', 'utf8')" at the end all the data disappears leaving the brackets..
Is the any function that can do a simple straight conversion to text so that I can print the data to a file with out massaging the heck out of the data?
Python 2.6, Windows XP Pro, Active directory
I'm trying this in Python to try to introduce an Open Source programing language where I work.
•
•
Join Date: Dec 2006
Posts: 1,041
Reputation:
Solved Threads: 293
1
#2 Oct 22nd, 2009
You can set the encoding for the file
fp = codecs.open('test', encoding='utf-8', mode='w')
Or you can change the default encoding for the system
sys.setdefaultencoding('utf-8')
You may have to use encode/decode, see here
http://farmdev.com/talks/unicode/
fp = codecs.open('test', encoding='utf-8', mode='w')
Or you can change the default encoding for the system
sys.setdefaultencoding('utf-8')
You may have to use encode/decode, see here
http://farmdev.com/talks/unicode/
Linux counter #99383
![]() |
Similar Threads
- Help-Problem In loading text file data in listview (Visual Basic 4 / 5 / 6)
- First step in reading a text file and converting it to a binary file (C++)
- Writing to a text file inside a class (Python)
- Writing to Text file: no duplicates (PHP)
- Problem Writing in Text File (C++)
- Reading/Writing To Text File Error (C++)
- Code Snippet: delete a line from a text file (C++)
- Writing data into a file (C)
- Trying to obtain text from a file. (C#)
Other Threads in the Python Forum
- Previous Thread: inheritance problem in python. help please.
- Next Thread: how to develop small games using python on mac
| Thread Tools | Search this Thread |
abrupt ansi anti approximation assignment avogadro backend beginner binary bluetooth calculator character cmd code customdialog data decimals dictionaries dictionary drive dynamic error examples excel exe file float format function gnu graphics gui heads homework http ideas import input java launcher leftmouse line linux list lists logging loop module mouse number numbers output parsing path pointer port prime programming progressbar projects push py2exe pygame pyqt python random recursion schedule script scrolledtext sqlite statistics stdout string strings sudokusolver sum table terminal text thread threading time tkinter tlapse tricks tuple tutorial twoup ubuntu unicode update urllib urllib2 variable wikipedia windows write wxpython xlib






