简体   繁体   English

使用 Python 生成随机 Json 数据并存储在变量中

[英]Generate Random Json data using Python and store in a varaible

My app takes JSON in this format.我的应用程序采用这种格式的 JSON。 How to use for loop to generate data in the mentioned format below, and store it in a variable?如何使用for循环生成下面提到的格式的数据,并将其存储在变量中?

Example with id='123456' : id='123456'示例:

{
    "abo": [

        {
            "id": "1ab",
            "data": fake.text(),
            "place": fake.place(),
            "user_id": id,
        },

        {
            "id": "1ab",
            "data": fake.text(),
            "place": fake.place(),
            "user_id": id,
        }
    ],
    "id": id
}

How to generate random data from this format and store them in a variable by given range?如何从此格式生成随机数据并将它们存储在给定范围的变量中?

If range is given as 3 ( for i in range(3) ) it should dump the following.如果 range 给出为 3 ( for i in range(3) ),它应该转储以下内容。



{
    "abo": [

        {
            "id": "1ab",
            "data": this has been a great day,
            "place": texas,
            "user_id": '123456',
        },
        {
            "id": "1ab",
            "data": Gaurd should be credited,
            "place": newyork,
            "user_id": '123456',
        },
        {
            "id": "1ab",
            "data": fake.text(),
            "place": fake.place(),
            "user_id": '123456',
        }
    ],
    "id": '123456'
}

Tried this, but this isn't correct way to use json:试过这个,但这不是使用 json 的正确方法:

import json 
from faker import Faker
import random
from random import randint
print('{"abo": [')
fake = Faker('en_US')
for _ in range(20):
       data=
       {
            "id": "1ab",
            "data": fake.text(),
            "place": fake.place(),
            "user_id": id,
        }
    print(",")
    print(json.dumps(abo))

print('],"id": id}')


You are pretty close.你很接近。 Declare the list and append data to it.向其声明列表和 append 数据。

Ex:前任:

from faker import Faker

fake = Faker('en_US')

n = 3
id='123456'
data = { "abo": [], "id": id}        #!Update
for _ in range(n):
    data["abo"].append(              #!Update
        {
            "id": "1ab",
            "data": fake.text(),
            "place": fake.state(),   #!Update
            "user_id": id
            }
        )
print(data)

Output: Output:

{'abo': [{'data': 'Discuss glass trial game physical pressure. Task former '
                  'perhaps suggest your. Some that suddenly family '
                  'organization north assume.',
          'id': '1ab',
          'place': 'Texas',
          'user_id': '123456'},
         {'data': 'Brother energy expect check maybe itself attack. Power size '
                  'we tell.',
          'id': '1ab',
          'place': 'Alaska',
          'user_id': '123456'},
         {'data': 'Result become politics each line price describe. Magazine '
                  'vote society life dark physical although. Position read '
                  'president card away. Since strong opportunity morning '
                  'would.',
          'id': '1ab',
          'place': 'Ohio',
          'user_id': '123456'}],
'id': '123456'}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM