首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >使用python将shell命令数组输出到csv列?

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

Stack Overflow用户
提问于 2018-12-12 10:09:05
回答 1查看 336关注 0票数 3

以下是'lastcomm‘bash命令的输出(日志)。

代码语言:javascript
复制
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中使用下面的代码得到了一个this。

代码语言:javascript
复制
import subprocess
file_ = open("pacct.csv", "w")
subprocess.Popen(['lastcomm'], stdout=file_)

我想分开的输出(日志)的列和保存csv文件的列结构。

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

如何使用python3将输出(日志)的元素按列拆分,并保存具有列结构的csv文件?

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

代码语言:javascript
复制
[['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'], ...]

非常感谢。

EN

回答 1

Stack Overflow用户

发布于 2018-12-12 11:27:48

来自manpage

代码语言:javascript
复制
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

很明显,lastcomm提供了commandflagsusertime

"__"只是占位符。你可以通过row.split("__")[1].lstrip(" ").rstrip("\n")获取时间

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

user做了类似的事情,但是颠倒了方向。

带空格的行包含的其余部分为flags

票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/53735085

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档