special charater in file causing trouble

Thomas Thomas thomas at mindz-i.co.nz
Tue Aug 1 22:29:39 EDT 2006


Hi All,

I need to process a file to create a hash.. the file contain a section
similar to 
string MetaDataPrompt = "Discovery No";

string MetaDataFieldName = "Discovery No";

string MetaDataType = "string";

string MetaDataValue = "£500";



everything was working fine until recently I got an error and found that it is due to "£" character and 

the error generated when processing the hash

ascii' codec can't decode byte 0xa3 in position 0: ordinal not in range(128)

I assume that this happening because the hash values I have are asciii encoded and I need to convert to unicode somehow..

i tried some googling and found http://www.jorendorff.com/articles/unicode/python.html

with not much luck in getting it work in my case.

metaValue=unicode('fieldValues[1], errors='ignore')


this will make the code work but i looses the "£" , I know that how it supposed to work



is there a way to handle such a situation  in python

code snippet

def _readXSTFilePoll(filename):
    metaHash={}
    if exists(filename):
       if isfile(filename):
           f = open(filename, "rb") 
           #infile = codecs.open(filename, "rb", encoding="utf-16")
           #text = infile.read()
           for line in f:
              if (line.find("string MetaData") >1):
                 metaSubHash={}
                 while(line.find("string MetaData")>1):
                    fieldValues=line.split('"')
                    extendedMetaKey=fieldValues[0].strip();
                    metaValue=fieldValues[1]
                    print metaValue
                    metaKeySplit=extendedMetaKey.split(' ')
                    metaKeyList=metaKeySplit[1].split('MetaData')
                    metaKey=metaKeyList[1]
                    metaSubHash[metaKey]=metaValue
                    if (metaKey=='Prompt'):
                       hashKey=metaValue
                       hashKey=hashKey.upper()
                    line=f.next()
                 metaHash[hashKey]=metaSubHash   
                 print "one section over"
           print filename,metaHash 
           return metaHash      
       else:
           return {}
           print "I cannot see a file of this name" 
    else:
        return {}



Thanks

-----------------------------------------------------
Thomas Thomas
thomas at mindz-i.co.nz
Phone.  +64 7 855 8478
Fax.      +64 7 855 8871
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/python-list/attachments/20060802/8a045f24/attachment.html>


More information about the Python-list mailing list