使用 re.sub 转换字符串大小写

import re

def pythonReSubDemo(inputStr):
"""
demo Pyton re.sub
"""
# inputStr = "recordInvalidDate"

def to_lower(matched):
    s = matched.group(0).lower()
    t = f'_{s}'
    return t

replaced_str = re.sub(r"([A-Z])", to_lower, inputStr)
"""
# 转换成字典
s_input = inputStr.strip()
s_replaced = replaced_str.strip()
dic = {
     s_input: s_replaced
}
print(dic)

"""

print('replaced_str', replaced_str)    # record_invalid_date  大写字母转成下划线 + 小写字母

if name == "main":
s1 = """
'economicType',
'companyHouseType',
'propertyType',
'propertyOwner',
'propertyDate',
'grossArea',
'systemSupplier',
'parkLocationClass',
'workerNum',
'parkCharger',
'mobilePhone',
'telPhone',

"""
s2 = s1.split(',')
for i in s2:
    pythonReSubDemo(i)
posted @ 2021-11-12 16:22  未来全栈攻城狮  阅读(265)  评论(0编辑  收藏  举报