在 json 中打印时如何添加双引号?目前我得到单引号
how to add double quotes while printing in json? Currently i am getting single quotes
代码
import json
from faker import Faker
import random
from random import randint
fake = Faker('en_US')
for _ in range(1):
sds = {
"id": "AB-asdfaf",
"body": fake.sentence(),
"time": fake.ean(),
"hash": fake.ean(),
"id1": fake.ean(),
"user_id": "asdasdas",
"id3": "test1"
}
print(sds)
我得到的输出是单引号 ''。我需要得到 json ijn 双引号
输出:
'body': 'Throughout third tough will PM time treat.'
我需要的输出:
"body": "Throughout third tough will PM time treat."
您可以使用:
replace('\'', '\"'))
如果您希望 JSON 输出正确,则必须显式转换数据:
print(json.dumps(sds))
不仅仅是引号,False
、True
和None
在[=中变成了false
、true
和null
21=].
配额不是字符串的一部分,print()
添加单个配额只是为了表明您有字符串 '123'
或数字 123
如果您使用模块 json
,那么您的格式会正确 JSON,双配额
print( json.dumps(sds) )
完整代码
import json
from faker import Faker
import random
from random import randint
fake = Faker('en_US')
for _ in range(1):
sds = {
"id": "AB-asdfaf",
"body": fake.sentence(),
"time": fake.ean(),
"hash": fake.ean(),
"id1": fake.ean(),
"user_id": "asdasdas",
"id3": "test1"
}
print(json.dumps(sds))
结果:
{"id": "AB-asdfaf", "body": "Buy government couple.", "time": "6066358112738", "hash": "1204203048039", "id1": "0212772145043", "user_id": "asdasdas", "id3": "test1"}
代码
import json
from faker import Faker
import random
from random import randint
fake = Faker('en_US')
for _ in range(1):
sds = {
"id": "AB-asdfaf",
"body": fake.sentence(),
"time": fake.ean(),
"hash": fake.ean(),
"id1": fake.ean(),
"user_id": "asdasdas",
"id3": "test1"
}
print(sds)
我得到的输出是单引号 ''。我需要得到 json ijn 双引号
输出:
'body': 'Throughout third tough will PM time treat.'
我需要的输出:
"body": "Throughout third tough will PM time treat."
您可以使用:
replace('\'', '\"'))
如果您希望 JSON 输出正确,则必须显式转换数据:
print(json.dumps(sds))
不仅仅是引号,False
、True
和None
在[=中变成了false
、true
和null
21=].
配额不是字符串的一部分,print()
添加单个配额只是为了表明您有字符串 '123'
或数字 123
如果您使用模块 json
,那么您的格式会正确 JSON,双配额
print( json.dumps(sds) )
完整代码
import json
from faker import Faker
import random
from random import randint
fake = Faker('en_US')
for _ in range(1):
sds = {
"id": "AB-asdfaf",
"body": fake.sentence(),
"time": fake.ean(),
"hash": fake.ean(),
"id1": fake.ean(),
"user_id": "asdasdas",
"id3": "test1"
}
print(json.dumps(sds))
结果:
{"id": "AB-asdfaf", "body": "Buy government couple.", "time": "6066358112738", "hash": "1204203048039", "id1": "0212772145043", "user_id": "asdasdas", "id3": "test1"}