0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

任意サイズ以上JSON作成python(調整しやすい)

Posted at
import json
import os

# 6MB以上のJSONファイルを生成する
def generate_large_json(filename, target_size_mb):
    data_list = []
    buffer_size = 10000  # バッファサイズを大きめに設定

    # 初期データを生成
    data = {
        "id": 0,
        "name": "example_name_0",
        "value": "example_value_0",
        "description": "This is a sample description to ensure the JSON file is large enough."
    }

    # ファイルを開く
    with open(filename, 'w', encoding='utf-8') as f:
        f.write('[')
        while os.path.getsize(filename) < target_size_mb * 1024 * 1024:
            for i in range(buffer_size):
                data["id"] += 1
                data["name"] = "example_name_" + str(data["id"])
                data["value"] = "example_value_" + str(data["id"])
                data_list.append(data.copy())

            # バッファをファイルに書き出し
            if data["id"] != buffer_size:
                f.write(',\n')
            f.write(',\n'.join(json.dumps(d, ensure_ascii=False) for d in data_list))
            data_list = []

        f.write(']')

# 6MB以上のファイルを生成
generate_large_json('large_data.json', 6)

# ファイルサイズを確認
file_size = os.path.getsize('large_data.json')
print(f"Generated JSON file size: {file_size / (1024 * 1024):.2f} MB")

0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?