944,125 Members | Top Members by Rank

Ad:
  • Python Discussion Thread
  • Unsolved
  • Views: 539
  • Python RSS
Oct 22nd, 2009
0

problem writing to a text file.

Expand Post »
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.
Similar Threads
Reputation Points: 10
Solved Threads: 0
Newbie Poster
nicsmr is offline Offline
1 posts
since Oct 2009
Oct 22nd, 2009
1
Re: problem writing to a text file.
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/
Reputation Points: 741
Solved Threads: 693
Nearly a Posting Maven
woooee is online now Online
2,308 posts
since Dec 2006
Oct 23rd, 2009
0
Re: problem writing to a text file.
Moderator
Reputation Points: 1333
Solved Threads: 1404
DaniWeb's Hypocrite
vegaseat is offline Offline
5,792 posts
since Oct 2004

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in Python Forum Timeline: inheritance problem in python. help please.
Next Thread in Python Forum Timeline: how to develop small games using python on mac





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC