【问题标题】:Python3: convert apostrophe unicode stringPython3:转换撇号 unicode 字符串
【发布时间】:2019-05-05 09:07:26
【问题描述】:

我有一个带撇号的字符串值,如下所示:

"I\\xE2\\x80\\x99m going now."

我怎样才能得到正确的撇号值?

"I`m going now."

如你所知,\xE2\x80\x99 是一个 unicode 字符 U+2019 RIGHT SINGLE QUOTATION MARK,但我有一个字符串表示而不是字节...

【问题讨论】:

    标签: python-3.x unicode-string unicode-escapes


    【解决方案1】:

    也许这就是你想要的:

    utf8_apostrophe = b'\xe2\x80\x99'.decode("utf8")
    str = "I"+utf8_apostrophe+"m going now"
    

    除此之外: 我在将 UTF-8 编码推文中的单引号转换为普通单引号时遇到了这个问题。

    import re
    original_tweet = 'I’m going now'
    string_apostrophe = "'"
    print re.sub(utf8_apostrophe, string_apostrophe, original_tweet)
    

    产生

    I'm going now
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2019-01-09
      • 1970-01-01
      • 2020-01-02
      • 2014-04-14
      • 2023-03-25
      • 1970-01-01
      • 2015-07-07
      • 1970-01-01
      相关资源
      最近更新 更多