linux - How to write the following shell command in python?
漂亮男人
漂亮男人 2017-06-28 09:23:05
0
3
1137

The original script was maintained by a senior. The shell script was too difficult to understand. The boss asked me to write it again in python. The function is as follows, which is to obtain the timestamp field in the svn log

]
I followed the python writing method, spliced ​​the commands together, and called popen to execute.

But an error was reported when running, as shown below:

Then I measured the front part of cmd, which is

The result is:

It can be seen that the result is correct, so the judgment problem occurs in the sed part. Please help me with your seniors.

漂亮男人
漂亮男人

reply all(3)
我想大声告诉你

Can it work if you copy the command directly to the command line? If it works, it means you made a mistake in organizing the string. For example, you missed the double quotes. For example, "%F" and '"%F"' in python are different, and '2' and '\2' are different. Different

刘奇

When I first started working, I was too stuck to other people’s ideas. I thought about using Python to implement functions as much as possible. Just use python’s replacement function to solve the problem

过去多啦不再A梦

No need to sed/grep...

# coding: utf8
import re 
from time import strftime, strptime
from subprocess import Popen, PIPE

cmd = 'svn log' # 不需要sed
result = Popen(cmd, shell=True, stdout=PIPE, stderr=PIPE)
stdout, stderr = result.communicate()

for _ in re.findall(r'(?<=date>)([^<.]+)\.', stdout):
    print(strftime("%F %T", strptime(_, "%Y-%m-%dT%H:%M:%S")))
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template