python - \1 (python) 的字符串替换
问题描述
我想删除下面字符串中 [] 内的逗号:
columns_data = '6, 7, 1729, 7, 7, [5, 6, 4, 6], [66, 55] ,45, 23' 我希望它是 '6, 7, 1729, 7, 7, [ 5 6 4 6], [66 55] ,45, 23'
我做了以下,但它不起作用......
re.sub('([[^[]*])', str(r'\1').replace(","," ") , columns_data ) '6, 7, 1729, 7, 7, [5 , 6, 4, 6], [66, 55] ,45, 23'
解决方案
尝试这个:
import re
text = '6, 7, 1729, 7, 7, [5, 6, 4, 6], [66, 55] ,45, 23'
re.sub(r'(\[.*?\])', lambda x: x.group().replace(',', ''), text)
输出:
'6, 7, 1729, 7, 7, [5 6 4 6], [66 55] ,45, 23'
推荐阅读
- python - Plotly:如何在不更改数据源的情况下更改 go.pie 图表的图例?
- javascript - 嵌套部分滚动 100vh
- amazon-web-services - 我们如何使用 AWS 控制台向 IAM 用户提供单个 EC2 实例的访问权限?
- javascript - Google 登录无法在隐身模式下工作
- r - Delta 方法和聚类标准误
- arrays - 将 bash 表输出映射到数组
- c++ - 为什么编译器/链接器会为我的函数生成“_16386”或“C3304_3”之类的“神秘”名称?
- python - 删除前一个对象后创建新的 timer_t 对象不起作用
- javascript - 拖放在编辑流程中无法正常工作
- php - 二叉树统计左右两边的所有子节点