You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Python实现:查找文件顶级任务并归集其缩进子任务

Fixing Task Grouping for Indented Task Files

Let's break down what's going wrong with your current code and how to fix it to group each top-level task with all its nested sub-tasks:

Issues in Your Original Code

  1. Incorrect Top-Level Task Detection: Your topboxes code collects any line starting with b'T0', including indented sub-tasks like b' T01_JOB5'. This means your list of top tasks includes non-top-level entries.
  2. Dictionary/List Confusion: You're using a dictionary (topboxesjobs) but trying to call append() on it, which will throw an error—dictionaries don't have an append method.
  3. Broken Grouping Logic: Looping through all top tasks for every line doesn't track which top task you're currently under, and the exit() call stops processing entirely after the first new top task is found.

Corrected Solution

Instead of pre-collecting top tasks, we'll track the current top-level task as we iterate through each line, adding sub-tasks to its group until we hit the next top task.

Full Working Code

# Replace this with your actual batchjobs data (e.g., from reading a file)
batchjobs = [
    b'T01_JOB1',
    b' T01_JOB1a',
    b' T01_JOB1b',
    b' T01_JOB1c',
    b'T01_JOB2',
    b' T01_JOB2a',
    b' T01_JOB2b',
    b'  T01_JOB2c',
    b'  T01_JOB2d',
    b'  T01_JOB2e',
    b'T01_JOB3',
    b'T01_JOB4',
    b' T01_JOB4a',
    b' T01_JOB4b',
    b' T01_JOB4c',
    b'  T01_JOB5',
    b'   T01_JOB5a',
    b'    T01_JOB5b',
]

task_groups = {}
current_top_task = None

for line in batchjobs:
    # Remove trailing newline characters from the line
    cleaned_line = line.rstrip(b'\n')
    
    # Check if this is a top-level task (starts with T0, no leading spaces)
    if cleaned_line.startswith(b'T0'):
        # Convert bytes to string for easier handling (skip if you need to keep bytes)
        current_top_task = cleaned_line.decode('utf-8')
        # Initialize an empty list to hold sub-tasks for this top task
        task_groups[current_top_task] = []
    else:
        # Skip empty lines if present in your file
        if not cleaned_line.strip():
            continue
        # Strip leading spaces from sub-tasks and convert to string
        sub_task = cleaned_line.lstrip(b' ').decode('utf-8')
        # Add the sub-task to the current top task's group
        if current_top_task is not None:
            task_groups[current_top_task].append(sub_task)

# Convert to the desired set of tuples format
task_set_collection = set()
for top_task, sub_tasks in task_groups.items():
    # Create a tuple with the top task followed by all its sub-tasks
    task_tuple = (top_task,) + tuple(sub_tasks)
    task_set_collection.add(task_tuple)

# Print the result to verify
for group in task_set_collection:
    print(group)

Key Improvements

  • Dynamic Top Task Tracking: We keep track of the current top-level task as we go, so every indented line gets added to the correct group.
  • Proper Data Structure: Uses a dictionary to map each top task to its list of sub-tasks, then converts it to the set of tuples you need.
  • Clean Sub-Task Handling: Strips leading spaces from sub-tasks to match your desired output format (remove the lstrip call if you want to keep leading spaces).
  • Error Resilience: Skips empty lines and handles cases where no top task is set (though your file shouldn't have sub-tasks before the first top task).

Output

Running this code will produce exactly the grouped task sets you want:

('T01_JOB1', 'T01_JOB1a', 'T01_JOB1b', 'T01_JOB1c')
('T01_JOB2', 'T01_JOB2a', 'T01_JOB2b', 'T01_JOB2c', 'T01_JOB2d', 'T01_JOB2e')
('T01_JOB3',)
('T01_JOB4', 'T01_JOB4a', 'T01_JOB4b', 'T01_JOB4c', 'T01_JOB5', 'T01_JOB5a', 'T01_JOB5b')

内容的提问来源于stack exchange,提问作者Demo

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.08.04 18:10:31