# -*- coding: utf-8 -*-
import csv
import json

if __name__ == '__main__':


    # 输入的CSV文件路径和输出的CSV文件路径
    input_csv_file = 'device_monitor_80.csv'  # 替换成你的输入CSV文件路径
    output_csv_file_8 = 'output_column_8.csv'
    output_csv_file_9 = 'output_column_9.csv'

    # 读取CSV文件，并提取第8列和第9列的JSON字段
    with open(input_csv_file, 'r') as infile, \
            open(output_csv_file_8, 'w') as outfile_8, \
            open(output_csv_file_9, 'w') as outfile_9:

        reader = csv.reader(infile)
        writer_8 = csv.writer(outfile_8)
        writer_9 = csv.writer(outfile_9)

        # 写入CSV文件的表头
        writer_8.writerow(["pid", "name", "cpuUsage", "memUsage"])
        writer_9.writerow(["pid", "name", "cpuUsage", "memUsage"])

        # 读取CSV文件的数据行
        for row in reader:
            json_data_8 = json.loads(row[7])  # 第8列的索引为7
            json_data_9 = json.loads(row[8])  # 第9列的索引为8

            # 写入第8列的数据
            for item in json_data_8:
                if item["name"] == "record":
                    writer_8.writerow([item["pid"], item["name"], item["cpuUsage"], item["memUsage"]])

            # 写入第9列的数据
            for item in json_data_9:
                if item["name"] == "record":
                    writer_9.writerow([item["pid"], item["name"], item["cpuUsage"], item["memUsage"]])

    print("CSV files generated successfully.")

