In [137]: log = open('/var/log/dpkg.log').read() In [137]: print(log) 2017-10-04 21:01:35 status half-configured libsox-fmt-mp3:i386 14.4.1-5 2017-10-04 21:01:35 status installed libsox-fmt-mp3:i386 14.4.1-5 2017-10-04 21:01:35 configure libsox-fmt-oss:i386 14.4.1-5 <none> 2017-10-04 21:01:35 status unpacked libsox-fmt-oss:i386 14.4.1-5 2017-10-04 21:01:35 status half-configured libsox-fmt-oss:i386 14.4.1-5 2017-10-04 21:01:35 status installed libsox-fmt-oss:i386 14.4.1-5 2017-10-04 21:01:35 configure libsox-fmt-pulse:i386 14.4.1-5 <none> 2017-10-04 21:01:35 status unpacked libsox-fmt-pulse:i386 14.4.1-5 2017-10-04 21:01:35 status half-configured libsox-fmt-pulse:i386 14.4.1-5 2017-10-04 21:01:35 status installed libsox-fmt-pulse:i386 14.4.1-5 2017-10-04 21:01:35 configure libsox-fmt-all:i386 14.4.1-5 <none> 2017-10-04 21:01:35 status unpacked libsox-fmt-all:i386 14.4.1-5 2017-10-04 21:01:35 status half-configured libsox-fmt-all:i386 14.4.1-5 2017-10-04 21:01:35 status installed libsox-fmt-all:i386 14.4.1-5 2017-10-04 21:01:35 configure sox:i386 14.4.1-5 <none> In [137]: import re In [141]: print(re.sub('(\d{4})-(\d{2})-(\d{2})',r'\2/\3/\1',log)) 另一种写法: print(re.sub(r'(\d{4})-(\d{2})-(\d{2})',r'\g<2>/\g<3>/\g<1>',log)) 10/04/2017 21:01:35 status unpacked libsox-fmt-pulse:i386 14.4.1-5 10/04/2017 21:01:35 status half-configured libsox-fmt-pulse:i386 14.4.1-5 10/04/2017 21:01:35 status installed libsox-fmt-pulse:i386 14.4.1-5 10/04/2017 21:01:35 configure libsox-fmt-all:i386 14.4.1-5 <none> 10/04/2017 21:01:35 status unpacked libsox-fmt-all:i386 14.4.1-5 10/04/2017 21:01:35 status half-configured libsox-fmt-all:i386 14.4.1-5 10/04/2017 21:01:35 status installed libsox-fmt-all:i386 14.4.1-5 10/04/2017 21:01:35 configure sox:i386 14.4.1-5 <none> 10/04/2017 21:01:35 status unpacked sox:i386 14.4.1-5 10/04/2017 21:01:35 status half-configured sox:i386 14.4.1-5 10/04/2017 21:01:35 status installed sox:i386 14.4.1-5 10/04/2017 21:01:35 trigproc libc-bin:i386 2.23-0ubuntu9 <none> 10/04/2017 21:01:35 status half-configured libc-bin:i386 2.23-0ubuntu9 10/04/2017 21:01:35 status installed libc-bin:i386 2.23-0ubuntu9 10/04/2017 21:01:35 startup packages configure print(re.sub('(?P<year>\d{4})-(?P<month>\d{2})-(?P<day>\d{2})',r'\g<month>/\g<day>/\g<year>',log)) 10/04/2017 21:01:35 status unpacked libsox-fmt-pulse:i386 14.4.1-5 10/04/2017 21:01:35 status half-configured libsox-fmt-pulse:i386 14.4.1-5 10/04/2017 21:01:35 status installed libsox-fmt-pulse:i386 14.4.1-5 10/04/2017 21:01:35 configure libsox-fmt-all:i386 14.4.1-5 <none> 10/04/2017 21:01:35 status unpacked libsox-fmt-all:i386 14.4.1-5 10/04/2017 21:01:35 status half-configured libsox-fmt-all:i386 14.4.1-5 10/04/2017 21:01:35 status installed libsox-fmt-all:i386 14.4.1-5 10/04/2017 21:01:35 configure sox:i386 14.4.1-5 <none> 10/04/2017 21:01:35 status unpacked sox:i386 14.4.1-5 10/04/2017 21:01:35 status half-configured sox:i386 14.4.1-5 10/04/2017 21:01:35 status installed sox:i386 14.4.1-5 10/04/2017 21:01:35 trigproc libc-bin:i386 2.23-0ubuntu9 <none> 10/04/2017 21:01:35 status half-configured libc-bin:i386 2.23-0ubuntu9 10/04/2017 21:01:35 status installed libc-bin:i386 2.23-0ubuntu9 10/04/2017 21:01:35 startup packages configure