Python - 检查字符串是否包含特殊字符,如果是,则替换它们

时间:2018-02-07 11:06:04

标签: python-3.x

我在循环中有很多字符串,有些字符串包含","有些不包含,我希望我的代码检查是否有任何","出现在字符串中删除它们然后打印字符串,如果它不存在,则按原样打印字符串。 这是我的代码:

for x in range(y):
c = containers[x].find("div",{"class":"cong-data"})
meeting = c.p.next_element
print(meeting)

提前致谢。

1 个答案:

答案 0 :(得分:0)

我建议使用正则表达式sub() function

import re

string = 'aabcdefg,hijklmnop,qrstu,ssads'
remove_commas = re.sub(',', '', string)

print(string)
print(remove_commas)

输出:

aabcdefg,hijklmnop,qrstu,ssads
aabcdefghijklmnopqrstussads
相关问题