如何用Python将FFmpeg视频缩放基准测试结果生成为指定表格?
Got it, let's turn those parsed dictionaries into the nested Markdown tables you need. We'll split each Parameter1 group with multi-hash separators, show Method A's results in a simple table, and build a nested-style table for Method B (with ParameterB2 as rows and ParameterB1 as columns). Here's a reusable Python script to generate the exact format:
def generate_markdown(A, B): markdown_output = "" # Iterate through each shared Parameter1 value for param1 in A.keys(): # Add multi-hash separator between Parameter1 blocks markdown_output += "\n##########\n\n" # Header for current Parameter1 group markdown_output += f"## Parameter1: {param1}\n\n" # Method A results table markdown_output += "### Method A\n" markdown_output += "| Parameter1 | Real Time (s) |\n" markdown_output += "|------------|---------------|\n" markdown_output += f"| {param1} | {A[param1]:.2f} |\n\n" # Method B nested table markdown_output += "### Method B\n" b_data = B[param1] # Get all unique ParameterB1 values for columns # Use this line if all ParameterB2 entries have the same ParameterB1s param_b1_cols = list(next(iter(b_data.values())).keys()) # Uncomment below if ParameterB1 values vary across ParameterB2 entries # param_b1_cols = list(dict.fromkeys(b1 for b2_dict in b_data.values() for b1 in b2_dict.keys())) # Build table header markdown_output += "| ParameterB2 | " + " | ".join(param_b1_cols) + " |\n" markdown_output += "|-------------|" + "|".join(["---" for _ in param_b1_cols]) + "|\n" # Build table rows for each ParameterB2 for param_b2, b1_times in b_data.items(): row_cells = [f"{param_b2}"] for b1 in param_b1_cols: # Format time to 2 decimal places, show "-" if missing time_val = b1_times.get(b1, "-") row_cells.append(f"{time_val:.2f}" if isinstance(time_val, float) else time_val) markdown_output += "| " + " | ".join(row_cells) + " |\n" markdown_output += "\n" # Remove the leading separator to clean up the start markdown_output = markdown_output.lstrip("\n##########\n\n") return markdown_output # Example usage with your parsed dictionaries 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}} } # Generate and print the markdown result_md = generate_markdown(A, B) print(result_md) # Optional: Write results to a markdown file with open("benchmark_results.md", "w") as f: f.write(result_md)
Key Details:
- The
##########lines act as clear separators between eachParameter1test block, as you requested. - Method A gets a compact single-row table to highlight its real-time performance for the current
Parameter1. - For Method B, we dynamically build columns from
ParameterB1values and rows fromParameterB2values, formatting all times to 2 decimal places for readability. If anyParameterB1value is missing for aParameterB2, it shows a-placeholder.
Example Output Snippet:
## Parameter1: a ### Method A | Parameter1 | Real Time (s) | |------------|---------------| | a | 4.03 | ### Method B | ParameterB2 | b11 | b12 | b13 | |-------------|---|---|---| | b21 | 0.00 | 0.00 | 0.00 | | b22 | 0.00 | 0.00 | 0.00 | | b23 | 0.00 | 0.00 | 0.00 | ########## ## Parameter1: b ### Method A | Parameter1 | Real Time (s) | |------------|---------------| | b | 3.23 | ### Method B | ParameterB2 | b11 | b12 | b13 | |-------------|---|---|---| | b21 | 0.00 | 0.00 | 0.00 | | b22 | 0.00 | 0.00 | 0.00 | | b23 | 0.00 | 0.00 | 0.00 |
内容的提问来源于stack exchange,提问作者Saurabh P Bhandari
相关产品推荐
相关产品推荐

