Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/338.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
GAE Python:将UTF-8字符从XML文件导入数据库模型_Python_Google App Engine_Utf 8_Getelementsbytagname - Fatal编程技术网

GAE Python:将UTF-8字符从XML文件导入数据库模型

GAE Python:将UTF-8字符从XML文件导入数据库模型,python,google-app-engine,utf-8,getelementsbytagname,Python,Google App Engine,Utf 8,Getelementsbytagname,我正在解析来自在线源的XML文件,但在读取utf-8字符时遇到问题。现在,我已经通读了一些处理类似问题的其他问题,但是到目前为止,没有一个解决方案有效。目前代码如下所示 class XMLParser(webapp2.RequestHandler): def get(self): url = fetch('some.xml.online') xml = parseString(url.content) vouchers = xml.getE

我正在解析来自在线源的XML文件,但在读取utf-8字符时遇到问题。现在,我已经通读了一些处理类似问题的其他问题,但是到目前为止,没有一个解决方案有效。目前代码如下所示

class XMLParser(webapp2.RequestHandler):

def get(self):

        url = fetch('some.xml.online')

        xml = parseString(url.content)

        vouchers = xml.getElementsByTagName("VoucherCode")

        for voucher in vouchers:

          if voucher.getElementsByTagName("ActivePartnership")[0].firstChild.data == "true":

            coupon = Coupon()
            coupon.description = str(voucher.getElementsByTagName("Description")[0].firstChild.data.decode('utf-8'))
            coupon.prov_key = str(voucher.getElementsByTagName("Id")[0].firstChild.data)
            coupon.put()
            self.redirect('/admin/coupon')
我从中得到的错误如下所示。这是由description字段中的一个“ü”引起的,稍后在使用数据时,我还需要显示该字段

文件“C:\Users\Vincent\Documents\www\Sparkompass\Website\main.py”,第217行,在get中 优惠券.description=str(凭证.getElementsByTagName(“description”)[0].firstChild.data.decode('utf-8')) 文件“C:\Python27\lib\encodings\utf_8.py”,第16行,解码 返回编解码器.utf_8_解码(输入,错误,真) UnicodeEncodeError:“ascii”编解码器无法对位置16中的字符u'\xfc'进行编码:序号不在范围内(128)

如果我去掉描述,一切都正常。在数据库模型定义中,我定义了如下描述:

description = db.StringProperty(multiline=True)
尝试2

我也尝试过这样做:

coupon.description = str(voucher.getElementsByTagName("Description")[0].firstChild.data).decode('utf-8')
这也给了我:

UnicodeEncodeError:“ascii”编解码器无法对位置16中的字符u'\xfc'进行编码:序号不在范围内(128)

任何帮助都将不胜感激

更新

XML文件包含德语,这意味着其中更多的字符是UTF-8字符。因此,理想情况下,我现在认为最好在更高级别上进行解码,例如在

xml = parseString(url.content)
然而,到目前为止,我也没有让它发挥作用。目的是获取ascii字符,因为GAE需要将其注册为数据库模型中的字符串

>>> u"ü".decode("utf-8")
独角兽

>>> u"ü".encode("utf-8") 
>>> str(u"ü".encode("utf-8"))
“\xc3\xbc”

>>> u"ü".encode("utf-8").decode("utf-8")
u'\xfc'

>>> str(u"ü".encode("utf-8").decode("utf-8"))
独角兽

>>> u"ü".encode("utf-8") 
>>> str(u"ü".encode("utf-8"))
“\xc3\xbc”

>>> u"ü".encode("utf-8").decode("utf-8")
您需要哪种编码

您还可以使用:

string2 = cgi.escape(string).encode("latin-1", "xmlcharrefreplace") 

这将所有非拉丁字符替换为xml实体。

我现在通过将描述更改为TextProperty解决了这个问题,但没有给出任何错误。我知道这样做时,我将无法进行排序或筛选,但对于描述,这应该是可以的


背景信息:

谢谢你的回答。我试图读取的XML文件包含的utf-8字符比这个多。我添加了一个更新的描述,希望能澄清。