import json | |
# JSONL 文件路径(替换为你的文件路径) | |
jsonl_file = "llama_finetune_data_reversed.jsonl" | |
def check_first_n_entries(file_path, n=10): | |
""" | |
检查 JSONL 文件中的前 n 条记录是否正确 | |
""" | |
try: | |
with open(file_path, 'r', encoding='utf-8') as f: | |
print(f"正在检查文件: {file_path}") | |
for i, line in enumerate(f): | |
if i >= n: # 只检查前 n 条记录 | |
break | |
line = line.strip() | |
if not line: # 跳过空行 | |
continue | |
# 解析 JSON | |
try: | |
entry = json.loads(line) | |
input_text = entry.get("input", None) | |
output_text = entry.get("output", None) | |
# 检查键是否存在以及内容是否为字符串 | |
if not isinstance(input_text, str) or not isinstance(output_text, str): | |
print(f"第 {i + 1} 条记录错误:键 'input' 或 'output' 缺失,或内容格式不正确") | |
else: | |
print(f"第 {i + 1} 条记录正确: input='{input_text[:50]}...' | output='{output_text[:50]}...'") | |
except json.JSONDecodeError: | |
print(f"第 {i + 1} 条记录错误:无法解析为合法 JSON") | |
except FileNotFoundError: | |
print(f"错误:文件 {file_path} 不存在,请检查路径") | |
except Exception as e: | |
print(f"发生异常: {e}") | |
def main(): | |
check_first_n_entries(jsonl_file) | |
if __name__ == "__main__": | |
main() | |