【问题标题】:Python - google content api and unicodePython - 谷歌内容 api 和 unicode
【发布时间】:2015-06-05 12:18:45
【问题描述】:

我已经搜索了很多页面来尝试帮助自己,以至于我现在对 python2 和 unicode 比开始之前更加困惑。

我想要达到的目标:

使用 google content api v2 for python,我编写了一个实现,它将从我们的数据库中获取产品并将它们发布到 Google。

这工作正常,直到我得到一些包含 unicode 字符的产品。

一个例子和从 google/python 返回的错误是:

D' Addario EXP11 Coated Bronze Acoustic Guitar Strings, 12-53 
Fender Stop Dreaming, Start Playing™ Affinity P Bass® With Rumble™ 15 

ERROR'utf8' codec can't decode byte 0x92 in position 1: invalid start byte
ERROR'utf8' codec can't decode byte 0x99 in position 35: invalid start byte

我知道它是 ' ® ™ 字符,但我不知道它的 .encode / .decode 等方面。

那么,谁能告诉我如何获取这些带有特殊字符的产品名称,以便我可以将它们发布到 Google?

== 更新 == 我从 MySQL 数据库中获取产品名称。该表设置为使用 UTF-8 作为编码。

【问题讨论】:

  • 还要确保将文件保存为 UTF-8 而不是其他编码。
  • 你的意思是把#coding=utf-8放在文件的顶部吗?
  • 否,就像将编辑器配置为另存为 UTF-8 一样。
  • @ColonelThirtyTwo 。感谢您提供信息,但我直接从 MySQL 获取这些产品名称,使用 UTF-8 作为 DB 编码类型。我的编辑将如何帮助我?
  • 不会的;我以为你在硬编码它们。无视我说的。

标签: python unicode google-content-api


【解决方案1】:

尝试:

u'Addario EXP11 Coated Bronze Acoustic Guitar Strings, 12-53 
Fender Stop Dreaming, Start Playing™ Affinity P Bass® With Rumble™ 15'

unicode('Addario EXP11 Coated Bronze Acoustic Guitar Strings, 12-53 
Fender Stop Dreaming, Start Playing™ Affinity P Bass® With Rumble™ 15')

但除此之外。 Python 2 中的 Unicode 支持很多时候都让人头疼。我建议尝试以 unicode 为标准的 Python 3。

【讨论】:

  • 我尝试了 unicode() 方法,但似乎不起作用,我似乎记得遇到了其他错误。
  • 当我通过 unicode() 方法发帖时,我会尝试挖掘问题,我也在尝试将其转换为 python3!
  • >>> mytext = unicode('Fender Stop Dreaming, Start Playing™ Affinity P Bass® With Rumble™ 15') Traceback(最近一次通话最后):文件“”,第 1 行,在 UnicodeDecodeError: 'ascii' codec can't decode byte 0xe2 in position 35: ordinal not in range(128)
【解决方案2】:

Python 3 就是答案 :)(现在 google 用他们的 sdk 支持它)

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-08-27
    • 1970-01-01
    • 1970-01-01
    • 2023-02-02
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多