Files
ai_crawler_tiktok/utils/io.py

54 lines
1.5 KiB
Python
Raw Normal View History

2025-12-08 15:20:22 +08:00
import json
import os
import csv
def load_keywords_from_file(path):
"""逐行读取关键词文件,忽略空行,返回列表"""
arr = []
try:
with open(path, 'r', encoding='utf-8') as f:
for line in f:
s = line.strip()
if s:
arr.append(s)
except Exception:
arr = []
return arr
def write_json(path, obj):
"""以 UTF-8 写入 JSON使用非 ASCII 保留与缩进"""
with open(path, 'w', encoding='utf-8') as f:
json.dump(obj, f, ensure_ascii=False, indent=2)
def read_json(path):
"""读取 JSON 文件,失败时返回空对象"""
try:
with open(path, 'r', encoding='utf-8') as f:
return json.load(f)
except Exception:
return {}
def ensure_csv_header(path, headers):
"""若 CSV 不存在则创建并写入表头;为空路径直接返回"""
if not path:
return
if not os.path.exists(path):
with open(path, 'w', newline='', encoding='utf-8') as wf:
w = csv.writer(wf)
w.writerow(headers)
def append_csv_rows(path, rows):
"""向 CSV 追加多行,行元素按列表给出;为空路径直接返回"""
if not path:
return
with open(path, 'a', newline='', encoding='utf-8') as af:
w = csv.writer(af)
for r in rows:
w.writerow(r)
"""通用 IO 工具
提供
- 关键词文件加载
- JSON 读写
- CSV 文件写入确保表头追加行
"""