python - 缩进 beetwen 键和值
问题描述
我想用正则表达式在字典中编辑密码和描述之间的相同间距。现在看起来像这样:
ADRIAN, Latin given name Adrianus or Hadrianus,
ADRIAN, a city in the U.S. state of Georgia,
ADRIAN, an unincorporated community in Rock Creek Township, Hancock County, Illinois,
应该是这样的:
ADRIAN, Latin given name Adrianus or Hadrianus,
ADRIAN, a city in the U.S. state of Georgia,
ADRIAN, an unincorporated community in Rock Creek Township, Hancock County, Illinois,
我试过:
import re
text = '''
ADRIAN, Latin given name Adrianus or Hadrianus,
ADRIAN, a city in the U.S. state of Georgia,
ADRIAN, an unincorporated community in Rock Creek Township, Hancock County, Illinois,
'''
p = re.compile("[^AZ][,]$\t{4}(az)") for line in text: print(line)
并获得结果 NONE 或 Process finished with exit code 0
解决方案
import re
text = """
ADRIAN, Latin given name Adrianus or Hadrianus,
ADRIAN, a city in the U.S. state of Georgia,
ADRIAN, an unincorporated community in Rock Creek Township, Hancock County, Illinois,
"""
new_text = re.sub(r"(?<=ADRIAN,)\s+", "\t\t", text)
print(new_text)
输出:
ADRIAN, Latin given name Adrianus or Hadrianus,
ADRIAN, a city in the U.S. state of Georgia,
ADRIAN, an unincorporated community in Rock Creek Township, Hancock County, Illinois,
推荐阅读
- c# - 如何编写控制器以将图像 Web 路径存储在数据库中
- ms-access - 将记录显示为列,将字段显示为行。
- xpages - 重复控制按钮 CSJS 不起作用
- android - 无法使用 minifyEnabled true 在调试和发布模式下运行 apk
- python - 获取每一行的值并在 Pandas 中创建一列
- java - 删除设备所有者 Android
- sql-server - SqlServer 中的会话特定表(临时表除外)
- android - 在 oreo 上安装未知 apk 时遇到问题
- powershell - 逐行比较 2 个 CSV 文件并仅打印新行
- javascript - 如何使用原生 JavaScript 代码重新启动或终止 Google Chrome?