tools / utils /json /jsonl.py
Adinosaur's picture
Upload folder using huggingface_hub
1c980b1 verified
raw
history blame contribute delete
994 Bytes
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}")