Python如何调整字符串中文本的格式

Jackey Python 1,574 次浏览 , 没有评论
# 如何调整字符串中文本的格式
log = open('../test.log').read()

import re

# 将日期2021-04-23 替换成 04/23/2021 格式
# r代表原始字符串,转义字符在这里作为普通字符串使用
# res = re.sub('(\d{4})-(\d{2})-(\d{2})', r'\2/\3/\1', log)
res = re.sub('(?P<year>\d{4})-(?P<month>\d{2})-(?P<day>\d{2})', r'\g<month>\g<day>\g<year>', log)

print(res)

 

发表回复

您的电子邮箱地址不会被公开。 必填项已用 * 标注

Go