【问题标题】:Replace '\xa0' in string with spaces用空格替换字符串中的 '\xa0'
【发布时间】:2018-08-22 13:56:19
【问题描述】:

我的字符串是'MRK\xa0Software\xa0Services\xa0Private\xa0Limited' 我想用空格替换十六进制部分(\xa0),这样我就得到了

MRK Software Services Private Limited 我可以进一步拆分成不同的词。

【问题讨论】:

  • 了解str对象的replace方法。
  • 为什么? '\xa0' 是一个不间断的空格。打印时它看起来像一个空格。它用于多词名称中,以防止它们被自动换行程序分解。
  • \xa 不是hexadecimal
  • @PM2Ring 我想将它们拆分为不同的单词,但拆分或替换部分不起作用。他们显示 unicode 错误
  • 如果你想分割那个角色,为什么不直接做test_string.split('\xa0')

标签: python regex python-3.x


【解决方案1】:
myString = "MRK\xa0Software\xa0Services\xa0Private\xa0Limited"

newString = myString.replace("\xa0", " ")

如果遇到 unicode 错误,可以尝试:

newString = myString.replace("\\xa0", " ")

【讨论】:

  • \xa0,而不是\xa
  • 是的,你需要逃避 \
  • 嗯,不,你应该逃避`\`。
  • 如果输入字符串是r"MRK\xa0Software\xa0Services\xa0Private\xa0Limited"myString.replace("\\xa0", " ") 会起作用。但事实并非如此。
  • 对我有用吗?
猜你喜欢
  • 2013-05-09
  • 1970-01-01
  • 1970-01-01
  • 2015-08-03
  • 1970-01-01
  • 2018-12-01
  • 2020-12-19
  • 1970-01-01
  • 2021-05-11
相关资源
最近更新 更多