export.py 1.9 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162
  1. import argparse
  2. import json
  3. from pathlib import Path
  4. from typing import Any, List, Optional, Union
  5. def build_lines_from_demand_items(input_path: Union[str, Path]) -> List[str]:
  6. """读取 demand items JSON,并格式化为逐行文本。"""
  7. path = Path(input_path)
  8. data: Any = json.loads(path.read_text(encoding="utf-8"))
  9. if not isinstance(data, list):
  10. raise ValueError(f"JSON 顶层必须是 list,实际是: {type(data).__name__}")
  11. lines: List[str] = []
  12. for idx, item in enumerate(data):
  13. if not isinstance(item, dict):
  14. raise ValueError(f"第 {idx + 1} 条记录不是对象: {type(item).__name__}")
  15. name = item.get("name", [])
  16. reason = str(item.get("reason", ""))
  17. suggestion = str(item.get("suggestion", ""))
  18. score = item.get("score", 0)
  19. ext_data = json.loads(item.get("ext_data", '{}'))
  20. type = ext_data.get('type')
  21. video_ids = ext_data.get('video_ids', [])
  22. lines.append(f"{name}|{type}|{reason}|{suggestion}|{score}|{video_ids}")
  23. return lines
  24. def export_lines(
  25. input_path: Union[str, Path],
  26. output_path: Optional[Union[str, Path]] = None,
  27. ) -> List[str]:
  28. """
  29. 读取 JSON 并导出为逐行结果。
  30. - output_path 为空: 打印到标准输出
  31. - output_path 不为空: 写入文件(UTF-8)
  32. """
  33. lines = build_lines_from_demand_items(input_path)
  34. text = "\n".join(lines)
  35. if output_path is None:
  36. print(text)
  37. else:
  38. Path(output_path).write_text(text, encoding="utf-8")
  39. return lines
  40. def main() -> None:
  41. account_name = 'R_50*泛知识*生活科普'
  42. input_path = f'/Users/shimeng/Desktop/py/Agent/examples/demand/result/{account_name}.json'
  43. output_path = f'/Users/shimeng/Desktop/py/Agent/examples/demand/result/{account_name}.txt'
  44. export_lines(input_path,
  45. output_path)
  46. if __name__ == "__main__":
  47. main()