如何替换字符串中除方括号和反斜杠以外的所有标点符号?

2024-06-25 07:26:18 发布

您现在位置:Python中文网/ 问答频道 /正文

例如,如果我有

'address": [{"type": null, "value": "1600 Amphitheatre Parkway\nMountain View\nCA\n94043\nUnited States"}]',

我希望是这样

'address [type null value 1600 Amphitheatre Parkway\nMountain View\nCA\n94043\nUnited States]'

Tags: viewvalueaddresstypenullstatesnunitedamphitheatre
2条回答

您只需按以下方式使用“替换”:

my_str = 'define_your_string_here'
to_be_replaced = [':','\"','{','}']
for rep in to_be_replaced:
    my_str = my_str.replace(rep, '')

您可以使用Python的String的^{}方法

>>> YourString = 'address": [{"type": null, "value": "1600 Amphitheatre Parkway\nMountain View\nCA\n94043\nUnited States"}]'
>>> YourString.replace('"', '').replace('{', '').replace('}', '').replace(':', '').replace(',', '')
'address [type null value 1600 Amphitheatre Parkway\nMountain View\nCA\n94043\nUnited States]'

相关问题 更多 >