UnicodeEncodeError: 'ascii' codec can't encode character u'\xef' in position 0: ordinal not in range(128)

前端 未结 7 1574
旧巷少年郎
旧巷少年郎 2020-11-28 04:24

I want to parse my XML document. So I have stored my XML document as below

class XMLdocs(db.Expando):  
   id = db.IntegerProperty()    
   name=db.StringPro         


        
7条回答
  •  温柔的废话
    2020-11-28 04:46

    It seems you are hitting a UTF-8 byte order mark (BOM). Try using this unicode string with BOM extracted out:

    import codecs
    
    content = unicode(q.content.strip(codecs.BOM_UTF8), 'utf-8')
    parser.parse(StringIO.StringIO(content))
    

    I used strip instead of lstrip because in your case you had multiple occurences of BOM, possibly due to concatenated file contents.

提交回复
热议问题