Blogger Information
Blog 41
fans 0
comment 1
visits 40532
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
Python高效编程技巧实战(12)
yeyiluLAMP
Original
641 people have browsed it

snipaste20170927_231650.png


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


Statement of this Website
The copyright of this blog article belongs to the blogger. Please specify the address when reprinting! If there is any infringement or violation of the law, please contact admin@php.cn Report processing!
All comments Speak rationally on civilized internet, please comply with News Comment Service Agreement
0 comments
Author's latest blog post