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

使用Python处理Autosys JIL文件生成指定格式Excel

解析Autosys JIL文件生成指定格式Excel的Python实现

需求说明

解析给定的Autosys JIL文件,提取每个任务的任务名称、所属容器、依赖条件三个字段,生成符合指定格式的Excel文件。

实现代码

使用pandas处理数据、openpyxl作为Excel导出引擎,代码如下:

import pandas as pd

def parse_jil_file(jil_content):
    # 按行处理JIL内容,过滤空行和注释块
    lines = [line.strip() for line in jil_content.split('\n') if line.strip() and not line.strip().startswith('/*')]
    job_blocks = []
    current_job = {}
    
    for line in lines:
        if line.startswith('insert_job:'):
            # 触发新任务时,保存上一个任务数据
            if current_job:
                job_blocks.append(current_job)
            current_job = {}
            # 提取任务名称和类型
            parts = line.split()
            current_job['Job'] = parts[1]
            job_type = parts[3]
            # 根BOX的所属容器字段填"Box"
            if job_type == 'BOX' and 'box_name' not in current_job:
                current_job['box_name'] = 'Box'
        elif line.startswith('box_name:'):
            current_job['box_name'] = line.split(':', 1)[1].strip()
        elif line.startswith('condition:'):
            current_job['condition'] = line.split(':', 1)[1].strip()
    
    # 添加最后一个任务的数据
    if current_job:
        job_blocks.append(current_job)
    
    # 补全缺失字段,确保格式统一
    for job in job_blocks:
        if 'condition' not in job:
            job['condition'] = ''
        if 'box_name' not in job:
            job['box_name'] = ''
    
    return pd.DataFrame(job_blocks, columns=['Job', 'box_name', 'condition'])

# 读取JIL内容(若内容保存在本地文件,可替换为文件读取逻辑)
jil_text = """    /* ----------------- Box1 ----------------- */
insert_job: Box1   job_type: BOX 
permission: 
date_conditions: 0
alarm_if_fail: 1

/* ----------------- Job2 ----------------- */ 

insert_job: Job2   job_type: POJO 
box_name: Box1
machine: testm
permission: 
date_conditions: 0
condition: s(Job5) & s(Job6) & s(Job7)
alarm_if_fail: 1
alarm_if_terminated: 1
application: Databricks
send_notification: 0
notification_msg: "Job2 Status"
method_name: runJob
j2ee_parameter: String=https://sample.com
j2ee_parameter: String=32322sdd
j2ee_parameter: String=12
j2ee_parameter: int=120
j2ee_parameter: long=30000


/* ----------------- Job3 ----------------- */ 

insert_job: Job3   job_type: POJO 
box_name: Box1
machine: testm
permission: 
date_conditions: 0
condition: s(Box10) & s(Box11) & s(Box12) & s(Job40) & s(Box13)
alarm_if_fail: 1
alarm_if_terminated: 1
application: Databricks
send_notification: 0
notification_msg: "Job3 Status"
method_name: runJob
j2ee_parameter: String=https://sample.com
j2ee_parameter: String=fgfgf1
j2ee_parameter: String=002
j2ee_parameter: int=120
j2ee_parameter: long=30000


/* ----------------- Box2 ----------------- */ 

insert_job: Box2   job_type: BOX 
box_name: Box1
permission: 
date_conditions: 0
condition: s(Job2)
alarm_if_fail: 1
alarm_if_terminated: 1
application: Databricks
"""

# 解析数据并生成Excel
df = parse_jil_file(jil_text)
df.to_excel('autosys_job_summary.xlsx', index=False, engine='openpyxl')
print("Excel文件生成成功:autosys_job_summary.xlsx")

使用说明

  1. 先安装依赖库:执行pip install pandas openpyxl
  2. 若JIL内容保存在本地文件(如jobs.jil),可将读取部分替换为:
    with open('jobs.jil', 'r', encoding='utf-8') as f:
        jil_text = f.read()
    
  3. 运行代码后,当前目录会生成autosys_job_summary.xlsx,格式完全匹配需求。

内容的提问来源于stack exchange,提问作者SK ASIF ALI

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.07.24 18:03:32