Spaces:
Running
Running
File size: 1,174 Bytes
812906a |
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 31 32 33 34 35 36 37 |
import json
from pathlib import Path
def txt_to_json(input_path: Path, output_path: Path) -> None:
"""
パイプ区切りのTXT形式ファイルをJSON形式に変換する。
:param input_path: 入力ファイルのパス(TXT形式)
:param output_path: 出力ファイルのパス(JSON形式)
"""
data_list: list[dict[str, str]] = []
with input_path.open(encoding="utf-8") as file:
for line in file:
line = line.strip()
if not line:
continue # 空行はスキップ
columns = line.split("|")
if len(columns) == 4:
key, company, name, url = columns
data_list.append(
{"key": key, "company": company, "name": name, "url": url}
)
# JSONファイルに書き込み
with output_path.open("w", encoding="utf-8") as json_file:
json.dump(data_list, json_file, ensure_ascii=False, indent=4)
# 使用例
input_file = Path("search_results.txt") # 入力ファイルのパス
output_file = Path("game_info.json") # 出力ファイルのパス
txt_to_json(input_file, output_file)
|