Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/python-3.x/17.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
使用python将shell命令数组输出到csv列?_Python_Python 3.x_Csv - Fatal编程技术网

使用python将shell命令数组输出到csv列?

使用python将shell命令数组输出到csv列?,python,python-3.x,csv,Python,Python 3.x,Csv,以下是“lastcomm”bash命令的输出(日志) python3 root __ 0.34 secs Tue Dec 11 09:06 python3 root __ 0.32 secs Tue Dec 11 09:06 python3 root __ 0.36 secs Tue Dec 11 09:06 cron

以下是“lastcomm”bash命令的输出(日志)

python3                root     __         0.34 secs Tue Dec 11 09:06
python3                root     __         0.32 secs Tue Dec 11 09:06
python3                root     __         0.36 secs Tue Dec 11 09:06
cron             SF    root     __         0.00 secs Tue Dec 11 09:06
sh               S     root     __         0.00 secs Tue Dec 11 09:06
python3                root     __         0.29 secs Tue Dec 11 09:06
cron             SF    root     __         0.00 secs Tue Dec 11 09:06
sh               S     root     __         0.00 secs Tue Dec 11 09:06
python3                root     __         0.30 secs Tue Dec 11 09:06
cron             SF    root     __         0.00 secs Tue Dec 11 09:06
sh               S     root     __         0.00 secs Tue Dec 11 09:06
python3                root     __         0.31 secs Tue Dec 11 09:06
cron             SF    root     __         0.00 secs Tue Dec 11 09:06
sh               S     root     __         0.00 secs Tue Dec 11 09:06
python3                root     __         0.28 secs Tue Dec 11 09:06
sh                     root     __         0.00 secs Tue Dec 11 09:06
uname                  root     __         0.00 secs Tue Dec 11 09:06
通过在python中使用以下代码,我得到了一个解决方案

import subprocess
file_ = open("pacct.csv", "w")
subprocess.Popen(['lastcomm'], stdout=file_)
我想按列分隔输出(日志),并使用列结构保存csv文件

但是上面的代码只保存完全相同输出(log)的纯文本。 输出的分隔符(分隔符)不是“制表符”,而是“不同大小的空间”,所以很难按列拆分列表

如何使用python3将输出(log)元素按列拆分,并使用列结构保存csv文件

预期结果: (如果我得到如下列表结构,我会将其转换为列结构csv文件。)

非常感谢。

来自

很明显,lastcomm提供了
命令
标志
用户
时间

只是一个占位符。您可以通过
row.split(“\uuu”)[1].lstrip(“”.rstrip(“\n”)

对于
命令
,从行开始搜索字符,直到出现前两个空格

用户
做相似但相反的事情

行的其余部分包含一个空格is
flags

[['python3', '', 'root', '_', '0.34 secs Tue Dec 11 09:06'],
['python3', '', 'root', '_', '0.32 secs Tue Dec 11 09:06'],
['python3', '', 'root', '_', '0.36 secs Tue Dec 11 09:06'],
['cron', 'SF', 'root', '_', '0.00 secs Tue Dec 11 09:06'],
['sh', 'S', 'root', '_', '0.00 secs Tue Dec 11 09:06'], ...]
For each entry the following information is printed:
          + command name of the process
          + flags, as recorded by the system accounting routines:
               S -- command executed by super-user
               F -- command executed after a fork but without a following
       exec
               C -- command run in PDP-11 compatibility mode (VAX only)
               D -- command terminated with the generation of a core file
               X -- command was terminated with the signal SIGTERM
          + the name of the user who ran the process
          + time the process started