【问题标题】:In Python, how to use re.sub() to replace all literal Unicode spaces?在 Python 中,如何使用 re.sub() 替换所有文字 Unicode 空格?
【发布时间】:2023-01-09 16:54:03
【问题描述】:

在 Python 中,当我使用 readlines() 从文本文件中读取时,原本是空格的东西将变成文字 Unicode 字符,如下所示。其中 \u2009 是原始文本文件中的一个空格。

因此,我使用 re.sub() 将这些 Unicode 文字空间替换为普通空间。

我的代码如下:

x = "Significant increases in all the lipoprotein fractions were observed in infected untreated mice compared with normal control mice. Treatment with 100 and 250\u2009mg/kg G. lucidum extract produced significant reduction in serum total cholesterol (TC) and low-density cholesterol (LDL-C) contents compared with 500\u2009mg/kg G. lucidum and CQ."

x = re.sub(r'[\x0b\x0c\x1c\x1d\x1e\x1f\x85\xa0\u1680\u2000\u2001\u2002\u2003\u2004\u2005\u2006\u2007\u2008\u2009\u200a\u2028\u2029\u202f\u205f\u3000]', " ", x)

不知道我说的对不对?

虽然程序看起来很正常,但我不确定,因为我对正则表达式的理解不够。

【问题讨论】:

    标签: python unicode


    【解决方案1】:

    快速解决方案:

    x = " ".join(x.split())
    

    【讨论】:

      猜你喜欢
      • 2012-11-24
      • 2015-01-13
      • 1970-01-01
      • 2013-05-01
      • 1970-01-01
      • 1970-01-01
      • 2017-10-15
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多