首页 > 解决方案 > 缩进 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

标签: pythonregex

解决方案


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,

推荐阅读