test3.py 1.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142
  1. # -*- coding: utf-8 -*-
  2. import csv
  3. import json
  4. if __name__ == '__main__':
  5. # 输入的CSV文件路径和输出的CSV文件路径
  6. input_csv_file = 'device_monitor_80.csv' # 替换成你的输入CSV文件路径
  7. output_csv_file_8 = 'output_column_8.csv'
  8. output_csv_file_9 = 'output_column_9.csv'
  9. # 读取CSV文件,并提取第8列和第9列的JSON字段
  10. with open(input_csv_file, 'r') as infile, \
  11. open(output_csv_file_8, 'w') as outfile_8, \
  12. open(output_csv_file_9, 'w') as outfile_9:
  13. reader = csv.reader(infile)
  14. writer_8 = csv.writer(outfile_8)
  15. writer_9 = csv.writer(outfile_9)
  16. # 写入CSV文件的表头
  17. writer_8.writerow(["pid", "name", "cpuUsage", "memUsage"])
  18. writer_9.writerow(["pid", "name", "cpuUsage", "memUsage"])
  19. # 读取CSV文件的数据行
  20. for row in reader:
  21. json_data_8 = json.loads(row[7]) # 第8列的索引为7
  22. json_data_9 = json.loads(row[8]) # 第9列的索引为8
  23. # 写入第8列的数据
  24. for item in json_data_8:
  25. if item["name"] == "record":
  26. writer_8.writerow([item["pid"], item["name"], item["cpuUsage"], item["memUsage"]])
  27. # 写入第9列的数据
  28. for item in json_data_9:
  29. if item["name"] == "record":
  30. writer_9.writerow([item["pid"], item["name"], item["cpuUsage"], item["memUsage"]])
  31. print("CSV files generated successfully.")