python - 生成基准表

标签 python python-3.x ffmpeg prettytable

我已经生成了基准,用于比较使用 ffmpeg 工具缩放视频文件所采用的两种方法。
基准以这种格式记录:

x.mp4 Output_Resolution : 10 p

Parameter1 : a

Method : A

real    0m5.788s
user    0m16.112s
sys     0m0.313s

Method : B, ParameterB1 : b11

ParameterB2 : b21

real    0m6.637s
user    0m16.618s
sys     0m0.720s

ParameterB2 : b22

real    0m5.486s
user    0m17.570s
sys     0m0.568s

ParameterB2 : b23

real    0m5.232s
user    0m18.212s
sys     0m0.718s

Method : B, ParameterB1 : b12

ParameterB2 : b21

real    0m6.398s
user    0m16.790s
sys     0m0.528s

ParameterB2 : b22

real    0m5.449s
user    0m17.229s
sys     0m0.533s

ParameterB2 : b23

real    0m5.275s
user    0m18.411s
sys     0m0.522s

##################################################################################################################

Parameter1 : b

Method : A

real    0m5.927s
user    0m16.451s
sys     0m0.308s

Method : B, ParameterB1 : b11

ParameterB2 : b21

real    0m6.685s
user    0m17.044s
sys     0m0.597s

ParameterB2 : b22

real    0m5.942s
user    0m18.971s
sys     0m0.804s

ParameterB2 : b23

real    0m6.119s
user    0m20.869s
sys     0m0.792s

.
.
.

有两种方法(A 和 B)。方法 A 和 B 共享 Parameter1 可以取值 a,b,c... .
方法 B 有其他参数 B1 和 B2。 ParameterB1 和 ParameterB2 取值 b11,b12,b13...b21,b22,b23...分别。行分隔符(由多个 # 组成)用于分隔 Parameter1 不同值的测量值。
我想以表格格式查看基准。
+--------+---------------------------------------+----------------+----------------+----------------+
| Method |                                       | Parameter1 (a) | Parameter1 (b) | Parameter1 (c) |
+--------+---------------------------------------+----------------+----------------+----------------+
|    A   |                   NA                  | 4.03s          | 3.23s          | 1.4s           |
+--------+-------------------+-------------------+----------------+----------------+----------------+
|    B   | ParameterB1 (b11) | ParameterB2 (b21) | .              |                |                |
|        |                   +-------------------+----------------+----------------+----------------+
|        |                   | ParameterB2 (b22) | .              |                |                |
|        |                   +-------------------+----------------+----------------+----------------+
|        |                   | ParameterB2 (b23) | .              |                |                |
|        +-------------------+-------------------+----------------+----------------+----------------+
|        | ParameterB1 (b12) | ParameterB2 (b21) | .              |                |                |
|        |                   +-------------------+----------------+----------------+----------------+
|        |                   | ParameterB2 (b22) | .              |                |                |
|        |                   +-------------------+----------------+----------------+----------------+
|        |                   | ParameterB2 (b23) | .              |                |                |
|        +-------------------+-------------------+----------------+----------------+----------------+
|        | ParameterB1 (b12) | ParameterB2 (b21) | .              |                |                |
|        |                   +-------------------+----------------+----------------+----------------+
|        |                   | ParameterB2 (b22) | .              |                |                |
|        |                   +-------------------+----------------+----------------+----------------+
|        |                   | ParameterB2 (b23) | .              |                |                |
+--------+-------------------+-------------------+----------------+----------------+----------------+
单元格值包含以秒为单位的实时值 (real 0m6.119s)。
如何使用 python 生成这样的表?

previous answer 的帮助下,我编写了一个“效率不高”的 Python 脚本。来自 similar question几个月前我问过。
import pprint

def gettime(x):
    m,s = map(float,x[:-1].split('m'))
    return 60 * m + s

with open("log") as fp:
    lines = fp.read().splitlines()

idx = 0
A = {}
B = {}

while idx < len(lines):
    if "Parameter1" in lines[idx]:
        Parameter1 = lines[idx].split(' ')[-1]
        temp1 = {}
        idx += 2
        if "A" in lines[idx]:
            idx += 2
            A[Parameter1] = gettime(lines[idx].split('\t')[-1])
            while idx < len(lines):
                if "B" in lines[idx]:
                    ParameterB1 = lines[idx].split(' ')[-1]
                    temp2 = {}
                    idx += 1
                    while idx < len(lines):
                        if "ParameterB2" in lines[idx]:
                            ParameterB2 = lines[idx].split(' ')[-1]
                            idx += 2
                            temp2[ParameterB2] = gettime(lines[idx].split('\t')[-1])
                        elif "#" in lines[idx] or "B" in lines[idx]:
                            break
                        idx += 1
                    temp1[ParameterB1] = temp2
                elif "#" in lines[idx]:
                    B[Parameter1] = temp1
                    break
                else:
                    idx += 1
    else:
        idx += 1
        
print("A")
print(A)

pp = pprint.PrettyPrinter(sort_dicts = False, depth = 4)
print("B")
pp.pprint(B)
此脚本解析日志并将为各个方法和参数获得的测量值存储在字典中。
脚本的示例输出:
A
{'a': 4.03, 'b': 3.23, 'c': 1.4}
B
{'a': {'b21': {'b11': 0.0, 'b12': 0.0, 'b13': 0.0},
       'b22': {'b11': 0.0, 'b12': 0.0, 'b13': 0.0},
       'b23': {'b11': 0.0, 'b12': 0.0, 'b13': 0.0}},
 'b': {'b21': {'b11': 0.0, 'b12': 0.0, 'b13': 0.0},
       'b22': {'b11': 0.0, 'b12': 0.0, 'b13': 0.0},
       'b23': {'b11': 0.0, 'b12': 0.0, 'b13': 0.0}},
 'c': {'b21': {'b11': 0.0, 'b12': 0.0, 'b13': 0.0},
       'b22': {'b11': 0.0, 'b12': 0.0, 'b13': 0.0},
       'b23': {'b11': 0.0, 'b12': 0.0, 'b13': 0.0}}}
如上所述,如何以表格格式打印它?

最佳答案

进一步扩展了python脚本(在问题中)以使用漂亮的表以表格格式表示存储在字典中的数据。

import pprint
import io
from prettytable import PrettyTable 
# install PTable package

def gettime(x):
    m,s = map(float,x[:-1].split('m'))
    return 60 * m + s

with open("log") as fp:
    lines = fp.read().splitlines()

idx = 0
A = {}
B = {}
Parameter1_list = []
ParameterB1_list = []
ParameterB2_list = []

while idx < len(lines):
    if "Parameter1" in lines[idx]:
        Parameter1 = lines[idx].split(' ')[-1]
        Parameter1_list.append(Parameter1)
        temp1 = {}
        idx += 2
        if "A" in lines[idx]:
            idx += 2
            A[Parameter1] = gettime(lines[idx].split('\t')[-1])
            while idx < len(lines):
                if "B" in lines[idx]:
                    ParameterB1 = lines[idx].split(' ')[-1]
                    ParameterB1_list.append(ParameterB1)
                    temp2 = {}
                    idx += 1
                    while idx < len(lines):
                        if "ParameterB2" in lines[idx]:
                            ParameterB2 = lines[idx].split(' ')[-1]
                            ParameterB2_list.append(ParameterB2)
                            idx += 2
                            temp2[ParameterB2] = gettime(lines[idx].split('\t')[-1])
                        elif "#" in lines[idx] or "B" in lines[idx]:
                            break
                        idx += 1
                    temp1[ParameterB1] = temp2
                elif "#" in lines[idx]:
                    B[Parameter1] = temp1
                    break
                else:
                    idx += 1
    elif ".mp4" in lines[idx]:
        title = lines[idx]
        idx += 1
    else:
        idx += 1

#print("A")
#print(A)

#pp = pprint.PrettyPrinter(sort_dicts=False,depth=4)
#print("B")
#pp.pprint(B)

Parameter1 = list(dict.fromkeys(Parameter1_list))
ParameterB1 = list(dict.fromkeys(ParameterB1_list))
ParameterB2 = list(dict.fromkeys(ParameterB2_list))

t1 = PrettyTable(['Method','ParameterB1','ParameterB2'])
t2 = PrettyTable(Parameter1)

t1.title = title
t2.title = "Parameter1"

t1.add_row(['A','NA','NA'])
t2.add_row(A.values())

for d in ParameterB1:
    for c in ParameterB2:
        values = []
        for e in Parameter1:
            values.append(B[e][d][c])
        t1.add_row(['B',d,c])
        t2.add_row(values)

o1 = io.StringIO(t1.get_string())
o2 = io.StringIO(t2.get_string())

with open(0,"w") as f1, open('result.txt',"w") as f2:
    for x,y in zip(o1,o2):
        f1.write(x.strip()[:-1] + y.strip() + "\n")
        f2.write(x.strip()[:-1] + y.strip() + "\n")
这会将表写入文件 (result.txt) 和标准输出。
输出 :
+------------------------------------+-------------------+
|   x.mp4  Output Resolution : 10p   |     Parameter1    |
+--------+-------------+-------------+------+------+-----+
| Method | ParameterB1 | ParameterB2 |  a   |  b   |  c  |
+--------+-------------+-------------+------+------+-----+
|   A    |      NA     |      NA     | 4.03 | 3.23 | 1.4 |
|   B    |     b11     |     b21     | 0.0  | 0.0  | 0.0 |
|   B    |     b11     |     b22     | 0.0  | 0.0  | 0.0 |
|   B    |     b11     |     b23     | 0.0  | 0.0  | 0.0 |
|   B    |     b12     |     b21     | 0.0  | 0.0  | 0.0 |
|   B    |     b12     |     b22     | 0.0  | 0.0  | 0.0 |
|   B    |     b12     |     b23     | 0.0  | 0.0  | 0.0 |
|   B    |     b13     |     b21     | 0.0  | 0.0  | 0.0 |
|   B    |     b13     |     b22     | 0.0  | 0.0  | 0.0 |
|   B    |     b13     |     b23     | 0.0  | 0.0  | 0.0 |
+--------+-------------+-------------+------+------+-----+
这是我在问题中描述的最接近表格格式的数据。

关于python - 生成基准表,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/62981516/

相关文章:

ffmpeg - 使用 ffmpeg 批量转换文件并找到 -exec 和并行

encoding - 使用 ffmpeg 进行多比特率编码

python - 将 CVS/SVN 转换为编程片段站点

python - 如何用列表中的逗号替换反斜杠

python-3.x - tkinter:拖动小部件

python - 在 Windows 上安装 TensorFlow (Python 3.6.x)

python - Anaconda 3 安装错误 - 没有脚本文件夹,也没有 conda 命令提示符和快捷方式

python - 基本的python算术——除法

python-3.x - 使用 boto3 将大量数据加载到 Dynamodb 非常慢

ffmpeg - 不支持压缩的 SWF 格式/cws2fws 问题