【问题标题】:Python - Copying from one text file to anotherPython - 从一个文本文件复制到另一个
【发布时间】:2019-07-02 21:40:22
【问题描述】:

我正在编写 Python 脚本,并且想要将灵活 I/O (FIO) 数据文本文件的特定行复制到我的输出文本文件中。

我尝试使用this tutorial 从我的数据文本文件中复制数据,但遗憾的是,我没有得到理想的结果。在我执行我的代码并连接到 output.txt 后,output.txt 什么都不返回,而不是 data.txt 中的 cpu 或 lat (usec) 的文本行。

这是我获取data.txt的代码:

#!/usr/bin/python
# -*- coding: utf-8 -*-

import subprocess
import os

cmd = subprocess.Popen(["sudo", "fio", "--filename=/dev/sdg", "--name=test1", "--ioengine=libaio",
    "--bs=4k", "--rw=read", "--iodepth=32", "--runtime=10"],
    stdout = subprocess.PIPE, stderr = subprocess.PIPE, preexec_fn = os.setpgrp)

f = open("logfile.txt", "w+")
g = open("logs.txt", "w+")

f.write(cmd.stdout.read().decode())

with g as f:
    for line in f:
        # finds the leading text in the text file that tarts with "", and also removes all
        # leading and trailing spaces from a strin.
        if line.strip().startswith("lat (usec)") or line.strip().startswith("cpu"):
            # adds items to end of list
            g.write(line + "\n")

这是我的数据文本文件(data.txt):

test1: (g=0): rw=read, bs=4K-4K/4K-4K/4K-4K, ioengine=libaio, iodepth=32
fio-2.16
Starting 1 process

test1: (groupid=0, jobs=1): err= 0: pid=9401: Tue Jul  2 14:28:30 2019
  read : io=4066.2MB, bw=416418KB/s, iops=104104, runt= 10001msec
    slat (usec): min=1, max=2650, avg= 7.75, stdev=41.93
    clat (usec): min=19, max=2790, avg=298.85, stdev=180.63
     lat (usec): min=101, max=2792, avg=306.59, stdev=181.00
    clat percentiles (usec):
     |  1.00th=[  105],  5.00th=[  112], 10.00th=[  118], 20.00th=[  120],
     | 30.00th=[  129], 40.00th=[  137], 50.00th=[  153], 60.00th=[  454],
     | 70.00th=[  474], 80.00th=[  490], 90.00th=[  502], 95.00th=[  516],
     | 99.00th=[  548], 99.50th=[  580], 99.90th=[  756], 99.95th=[  932],
     | 99.99th=[ 1592]
    lat (usec) : 20=0.01%, 100=0.01%, 250=51.56%, 500=36.57%, 750=11.76%
    lat (usec) : 1000=0.06%
    lat (msec) : 2=0.04%, 4=0.01%
  cpu          : usr=18.48%, sys=38.28%, ctx=16300, majf=0, minf=40
  IO depths    : 1=0.1%, 2=0.1%, 4=0.1%, 8=0.1%, 16=0.1%, 32=100.0%, >=64=0.0%
     submit    : 0=0.0%, 4=100.0%, 8=0.0%, 16=0.0%, 32=0.0%, 64=0.0%, >=64=0.0%
     complete  : 0=0.0%, 4=100.0%, 8=0.0%, 16=0.0%, 32=0.1%, 64=0.0%, >=64=0.0%
     issued    : total=r=1041149/w=0/d=0, short=r=0/w=0/d=0, drop=r=0/w=0/d=0
     latency   : target=0, window=0, percentile=100.00%, depth=32

Run status group 0 (all jobs):
   READ: io=4066.2MB, aggrb=416417KB/s, minb=416417KB/s, maxb=416417KB/s, mint=10001msec, maxt=10001msec

Disk stats (read/write):
  sdg: ios=16094/0, merge=0/0, ticks=14376/0, in_queue=14368, util=99.01%

现在我希望将数据文本文件中的第 8、15 和 18 行放到输出文本文件中,但它什么也没返回。这就是输出文本文件的样子(output.txt):

     lat (usec): min=101, max=2792, avg=306.59, stdev=181.00
    lat (usec) : 250=5.09%, 500=67.38%, 750=23.29%, 1000=3.87%
  cpu          : usr=18.48%, sys=38.28%, ctx=16300, majf=0, minf=40

有没有类似的方法可以使用上面的教程从我的数据文本文件中提取数据到我的结果输出文件中?

谢谢!

【问题讨论】:

  • 欢迎来到stackoverflow!请发布您已经拥有的代码,以便我们分析如何改进它。此外,请发布您想要解决的特定问题。您要复制哪些行?作为一个人类,你将如何识别正确的线条是什么?请详细说明。
  • @mstewart 请不要用机器抓取 fio 的人类可读输出 - 有很多方法会适得其反(参见 Brendan Gregg video about the perils of scraping fio output)!相反,您可以使用 fio 的 JSON 输出(或暂时使用其 CSV 输出)-fio.readthedocs.io/en/latest/… 吗?
  • @Anon Thaks 给小费!我对这个文件做了很多修改,最终使用 JSON 输出来访问 fio 输出中的特定内容。
  • @mstewart 不客气!我知道这是一个微妙的点,但它会节省很多“发生了什么?”稍后提问...
  • @Anon 非常正确,再次感谢!

标签: python io copy benchmarking text-extraction


【解决方案1】:

这样的?

with open("/tmp/log.txt") as f:
        to_keep = []
        for line in f:
                if line.strip().startswith("lat (usec)") or line.strip().startswith("cpu"):
                        to_keep.append(line)
        print(to_keep)

顺便说一句:这不是最干净的方法,但我猜是最简单的一种(在 python 中)

更简单的是使用简单的 linux 命令,例如:

   cat log.txt | grep "lat (usec)"

【讨论】:

  • 我要下班了,但我明天试试这个。感谢您的建议!
  • 另外,如果您要使用 unix 工具(即 cat 和 grep),您可以使用 subprocess.popen 管理它们。
  • 嗨@pkfm,感谢您的提示,但我无法为该脚本使用 unix 工具。
  • 嗨@AlessioM,您的代码有所帮助,因为我不再遇到任何错误,但是当我运行代码时,生成的日志文件仍然是空白的。编辑:删除链接
【解决方案2】:

我发现了我的问题,感谢@AlessioM 和@pkfm 的帮助!

这是我的答案:

#!/usr/bin/python
# -*- coding: utf-8 -*-

import subprocess
import os

def fioCall():
    cmd = subprocess.Popen(["sudo", "fio", "--filename=/dev/sdg", "--name=test1", "--ioengine=libaio",
    "--bs=4k", "--rw=read", "--iodepth=32", "--runtime=10"],
    stdout = subprocess.PIPE, stderr = subprocess.PIPE, preexec_fn = os.setpgrp)

    f = open("data.txt", "w+")
    f.write(cmd.stdout.read().decode())
    f.close()

def copyFiles():
    with open("data.txt") as f:
        with open("output.txt", "w") as f1:
            for line in f:
                if line.strip().startswith("lat (usec)") or line.strip().startswith("cpu"):
                    f1.write(line)

def main():
    fioCall()
    copyFiles()

if __name__ == "__main__":
    main()

这是我得到的输出(output.txt):

     lat (usec): min=161, max=10624, avg=458.78, stdev=148.10
    lat (usec) : 250=5.09%, 500=67.38%, 750=23.29%, 1000=3.87%
  cpu          : usr=17.24%, sys=63.28%, ctx=81571, majf=0, minf=40

【讨论】:

    猜你喜欢
    • 2013-02-26
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2023-01-14
    • 1970-01-01
    相关资源
    最近更新 更多