【问题标题】:I want to remove \u00a9 , \u201d and characters like that from my string in given dictionary (python).我想从给定字典(python)中的字符串中删除 \u00a9 , \u201d 和类似的字符。
【发布时间】:2014-06-14 10:08:44
【问题描述】:
d = {
    "key": "Impress the playing crowd with these classic "
           "Playing Cards \u00a9 Personalized Coasters.These beautiful"
           " coasters are made from glass, and measure approximately 4\u201d x 4\u201d (inches)"
           ".Great to look at, and lovely to the touch.There are 4 coasters in a set.We have "
           "created this exclusive design for all card lovers.Each coaster is a different suit, "
           "with the underneath.Make your next Bridge, or Teen Patti session uber-personal!"
           "Will look great on the bar, or any tabletop.Gift Designed for: Couples, Him, "
           "HerOccasion:Diwali, Bridge, Anniversary, Birthday"}

我已经尝试过替换功能,但没有用。

s = d[key].replace('\u00a9','')

【问题讨论】:

  • 请注意\u00a9 是版权符号。删除可能会产生法律后果。
  • 同意,删除您无法理解的字符似乎是解决此问题的错误方法。 (实际文本有更严重的问题,但我想这超出了本网站的范围。)
  • 请解释“但没用”。发生了什么?你预计会发生什么?
  • @André 写出来作为答案,这似乎是 OP 正在寻找的。​​span>
  • 不应该删除 unicode 字符 - 你必须使用 DFEAL - 请阅读:joelonsoftware.com/articles/Unicode.html

标签: python string dictionary


【解决方案1】:

如果要从字符串中删除所有 Unicode 字符,可以使用string.encode("ascii", "ignore")

它尝试将字符串编码为 ASCII,第二个参数 ignore 告诉它忽略它无法转换的字符(所有 Unicode 字符),而不是像通常没有第二个参数那样抛出异常,所以它返回一个只包含可以成功转换的字符的字符串,从而删除所有 Unicode 字符。

示例用法:

unicodeString = "Héllò StàckOvèrflow"
print(unicodeString.encode("ascii", "ignore")) # prints 'Hll StckOvrflow'

更多信息:Python 文档中的str.encode()Unicode

【讨论】:

    【解决方案2】:
    d['key'].decode('unicode-escape').encode('ascii', 'ignore')
    

    就是你要找的东西

    >>> d = {
    ...     "key": "Impress the playing crowd with these classic "
    ...            "Playing Cards \u00a9 Personalized Coasters.These beautiful"
    ...            " coasters are made from glass, and measure approximately 4\u201d x 4\u201d (inches)"
    ...            ".Great to look at, and lovely to the touch.There are 4 coasters in a set.We have "
    ...            "created this exclusive design for all card lovers.Each coaster is a different suit, "
    ...            "with the underneath.Make your next Bridge, or Teen Patti session uber-personal!"
    ...            "Will look great on the bar, or any tabletop.Gift Designed for: Couples, Him, "
    ...            "HerOccasion:Diwali, Bridge, Anniversary, Birthday"}
    >>> d['key'].decode('unicode-escape').encode('ascii', 'ignore')
    'Impress the playing crowd with these classic Playing Cards  Personalized Coasters.These beautiful coasters are made from glass, and measure approximately 4 x 4 (inches).Great to look at, and lovely to the touch.There are 4 coasters in a set.We have created this exclusive design for all card lovers.Each coaster is a different suit, with the underneath.Make your next Bridge, or Teen Patti session uber-personal!Will look great on the bar, or any tabletop.Gift Designed for: Couples, Him, HerOccasion:Diwali, Bridge, Anniversary, Birthday'
    >>> 
    

    【讨论】:

    • AttributeError: 'str' object has no attribute 'decode'
    • str 没有 decode() 方法。
    【解决方案3】:

    要删除由 unicode 转义序列表示的字符,您需要使用 unicode 字符串。

    例如,

    s = d[key].replace(u'\u00a9', '')
    

    但是,正如人们在 cmets 中提到的那样,删除版权符号可能是一个非常糟糕的主意,尽管这取决于您对字符串的实际操作。

    【讨论】:

    • (对于它的价值,d['key'].replace("\u00a9","") 在 Python3.3 上按预期工作)
    • @AdamSmith 它在 Python2.7 中有所作为
    • 是的,我在尝试 py2.7
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2021-12-15
    • 1970-01-01
    • 2012-05-06
    • 2017-09-21
    • 2011-04-09
    • 1970-01-01
    • 2011-04-25
    相关资源
    最近更新 更多