Python數(shù)據(jù)清洗之抽取jsonl文件數(shù)據(jù)字段并合并
基于python抽取目錄下所有“jsonl”格式文件。遍歷文件內某個字段進行抽取并合并。
實現(xiàn)代碼
import os import json import time from tqdm import tqdm # 需要先安裝:pip install tqdm def process_files(): # 設置目錄路徑 dir_path = r"D:\daku\關鍵詞識別\1623-0000001\zh" # 獲取并排序文件列表 file_list = sorted([f for f in os.listdir(dir_path) if f.lower().endswith('.jsonl')], key=lambda x: os.path.getsize(os.path.join(dir_path, x)), reverse=True) # 按文件大小降序排列 # 進度統(tǒng)計 total_files = len(file_list) processed_files = 0 total_lines = sum(1 for f in file_list for _ in open(os.path.join(dir_path, f), 'r', encoding='utf-8')) processed_lines = 0 start_time = time.time() # 輸出文件設置 output_file = os.path.join(dir_path, "combined_contents.txt") with open(output_file, "w", encoding="utf-8") as outfile: with tqdm(total=total_lines, desc="合并進度", unit="line") as pbar: for filename in file_list: file_path = os.path.join(dir_path, filename) try: with open(file_path, "r", encoding="utf-8") as infile: file_size = os.path.getsize(file_path) chunk_size = max(1024 * 1024, file_size // 100) # 動態(tài)調整讀取塊大小 while True: lines = infile.readlines(chunk_size) if not lines: break for line_num, line in enumerate(lines, 1): line = line.strip() if not line: continue try: data = json.loads(line) content = data.get("content", "").replace("\n", " ") # 清除內容中的換行符 outfile.write(content + "\n\n") # 用雙換行分隔記錄 processed_lines += 1 except json.JSONDecodeError: print(f"\nJSON解析失敗: {filename} 第{processed_lines + 1}行") except Exception as e: print(f"\n處理異常: {filename} 第{processed_lines + 1}行 - {str(e)}") # 進度更新 pbar.update(1) if processed_lines % 1000 == 0: elapsed = time.time() - start_time speed = processed_lines / (elapsed + 1e-5) remaining = (total_lines - processed_lines) / (speed + 1e-5) pbar.set_postfix({ '速度': f"{speed:.1f} lines/s", '剩余時間': f"{remaining // 3600:.0f}h {remaining % 3600 // 60:.0f}m" }) processed_files += 1 except Exception as e: print(f"\n無法讀取文件 {filename}: {str(e)}") # 生成統(tǒng)計報告 end_time = time.time() print(f"\n合并完成!共處理 {processed_files}/{total_files} 個文件") print(f"總記錄數(shù): {processed_lines:,} 條") print(f"耗時: {end_time - start_time:.2f} 秒") print(f"輸出文件路徑: {output_file}") if __name__ == "__main__": process_files()
知識延展:
1.Python中json文件和jsonl文件的區(qū)別
眾所周知,JSON 文件是使用 JSON(JavaScript Object Notation)格式存儲數(shù)據(jù)的文件。它是一種結構化的文本格式,使用鍵值對的方式表示數(shù)據(jù)。JSON 文件通常包含一個根對象,可以包含多個嵌套的對象、數(shù)組和基本數(shù)據(jù)類型。
而 JSONL 文件(JSON Lines),則是一種每行包含一個獨立的 JSON 對象的文本文件格式。每行都是一個有效的 JSON 對象,和json的"list dict"不一樣,jsonl的話沒有"list",只有一行行的“dict”,使用換行符分隔。相比于 JSON 文件,JSONL 文件更加輕量,每行為獨立的 JSON 對象,沒有逗號或其他分隔符。這樣的好處是方便一行行讀取,而不用像json一樣只能一次性把"list"里面的"dict”全部都讀出來,節(jié)省了內存吧,并且還能增加可讀性,普通的json文件打開之后會亂糟糟一團。jsonl的話需要pip安裝一個jsonlines包。
JSON文件的內容示例:
[{"name": "John", "age": 30}, {"name": "Jane", "age": 25}, {"name": "Bob", "age": 40}]
JSONL文件的內容示例:
{"name": "John", "age": 30} {"name": "Jane", "age": 25} {"name": "Bob", "age": 40}
主要區(qū)別如下:
JSON 文件:
- 使用大括號 {} 表示對象,使用方括號 [] 表示數(shù)組。
- 整個文件是一個有效的 JSON 對象或數(shù)組。
- 適合存儲結構化的數(shù)據(jù),如配置文件、API 響應等。
- 一次性讀取整個文件,解析成一個 JSON 對象,可以隨機訪問其中的數(shù)據(jù)。
JSONL 文件:
- 每行是一個獨立的有效 JSON 對象。
- 每行之間沒有逗號或其他分隔符。
- 適合存儲每行為獨立記錄的數(shù)據(jù),如日志、傳感器數(shù)據(jù)、日志行等。
- 逐行讀取文件,逐行解析 JSON 對象,一次處理一行的數(shù)據(jù)。
JSONL 文件適合用于以下情況:
- 當數(shù)據(jù)以行為單位獨立存儲,并且每行數(shù)據(jù)之間沒有明確的分隔符時。
- 當需要逐行處理數(shù)據(jù),以節(jié)省內存和提高處理速度時。
- 當數(shù)據(jù)量非常大,無法一次性加載到內存中時,JSONL 格式提供了一種流式處理數(shù)據(jù)的方式。
這么對比下來,JSON 文件更適合結構化的數(shù)據(jù)存儲和傳輸,而 JSONL 文件更適合每行為獨立記錄的數(shù)據(jù)存儲和處理。
到此這篇關于Python中json文件和jsonl文件的區(qū)別小結的文章就介紹到這了,更多相關Python json文件和jsonl文件區(qū)別內容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關文章希望大家以后多多支持腳本之家!
2.抽取多個文本格式內容進行合并
即抽取目錄下多個文本多個格式文件,進行合并并去重。
實現(xiàn)代碼
import os from chardet import detect def get_safe_encoding(encoding): """將檢測到的編碼轉換為更安全的兼容編碼""" encoding = encoding.lower() if encoding in ['gb2312', 'gbk']: return 'gb18030' # 最全面的中文編碼 return encoding def get_file_encoding(file_path): """獲取文件編碼并自動升級到更安全的版本""" with open(file_path, 'rb') as f: raw_data = f.read(10000) result = detect(raw_data) # 過濾低置信度檢測(confidence<0.8則視為不可信) if result['confidence'] < 0.8: return 'gb18030' return get_safe_encoding(result['encoding']) def merge_files(directory, output_filename='merged.txt'): seen_lines = set() output_path = os.path.join(directory, output_filename) txt_files = [os.path.join(directory, f) for f in os.listdir(directory) if f.endswith('.txt')] with open(output_path, 'w', encoding='utf-8', errors='ignore') as outfile: for file_path in txt_files: try: # 獲取安全編碼并添加錯誤處理 file_enc = get_file_encoding(file_path) with open(file_path, 'r', encoding=file_enc, errors='backslashreplace') as infile: # 保留無法解碼字符 for line_idx, line in enumerate(infile, 1): try: stripped_line = line.rstrip('\n') if stripped_line not in seen_lines: outfile.write(line) seen_lines.add(stripped_line) except Exception as line_err: print(f"文件 {os.path.basename(file_path)} 第 {line_idx} 行處理異常: {str(line_err)}") continue except Exception as file_err: print(f"文件 {os.path.basename(file_path)} 讀取失敗: {str(file_err)}") continue if __name__ == '__main__': target_directory = r'D:\daku\關鍵詞識別\stop6931' merge_files(target_directory) print(f'合并完成,輸出文件:{os.path.join(target_directory, "merged.txt")}')
到此這篇關于Python數(shù)據(jù)清洗之抽取jsonl文件數(shù)據(jù)字段并合并的文章就介紹到這了,更多相關Python抽取jsonl數(shù)據(jù)字段內容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關文章希望大家以后多多支持腳本之家!
相關文章
利用python 更新ssh 遠程代碼 操作遠程服務器的實現(xiàn)代碼
這篇文章主要介紹了利用python 更新ssh 遠程代碼 操作遠程服務器的實現(xiàn)代碼,需要的朋友可以參考下2018-02-02Python基于DB-API操作MySQL數(shù)據(jù)庫過程解析
這篇文章主要介紹了Python基于DB-API操作MySQL數(shù)據(jù)庫過程解析,文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友可以參考下2020-04-0420個Python?random模塊常用函數(shù)的應用與代碼示例
隨機數(shù)在計算機科學和數(shù)據(jù)科學領域中扮演著重要角色,Python的標準庫中提供了random模塊,用于生成各種隨機數(shù),本文將深入探討random模塊的各種函數(shù),以及它們的應用場景和代碼示例,需要的可以參考下2024-03-03