File size: 994 Bytes
1c980b1
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
import sys
import json

def count_jsonl_objects(file_path):
    count = 0
    with open(file_path, 'r', encoding='utf-8') as file:
        for line_number, line in enumerate(file, 1):
            stripped_line = line.strip()
            if not stripped_line:
                continue  # 跳过空行
            try:
                json.loads(stripped_line)
                count += 1
            except json.JSONDecodeError as e:
                print(f"解析第 {line_number} 行时发现无效JSON: {e}")
    return count

if __name__ == "__main__":
    if len(sys.argv) != 2:
        print("使用方法: python count_jsonl.py <文件路径>")
        sys.exit(1)
    
    file_path = sys.argv[1]
    try:
        total = count_jsonl_objects(file_path)
        print(f"文件 {file_path} 中共包含 {total} 个有效的JSON对象")
    except FileNotFoundError:
        print(f"错误:文件 {file_path} 未找到")
    except Exception as e:
        print(f"发生未知错误: {e}")