0

如何用逗号替换 Python2.7 中的这些字符:

| •</p>

像这样的东西不起作用:

a= b.replace("|", ",")

谢谢

4

1 回答 1

3

使用包含要替换的字符列表的正则表达式

import re
a = re.sub(u'[|•]', ',', a)

句法:

re.sub(pattern, repl, string, max=0)

此方法用 repl 替换字符串中所有出现的 RE 模式,除非提供了max ,否则替换所有出现。

编辑您必须在源文件的顶部声明它使用 Unicode 文字。

# -*- coding: utf-8 -*-

还使用u搜索前缀字符串

a = u"6• 918417•12"
a = re.sub(u"[|•]", ",", a)
于 2012-11-21T11:08:41.567 回答