我正在阅读具有布尔值的json。当使用加载Python中的文件时json.load()
,我得到了True
(我已经阅读了规范)。
{"XYZ": true}
据我了解,我需要使用它json.dumps()
来处理这个问题。我的转储看起来像这样。
convert = json.dumps({"true": True, "false": False})
而我的负载是这样的:
jdata = json.load(open(json_path))
打印时,jdata
我得到:
{u'XYZ': True}
我的问题是,我该如何转换True
到true
加载JSON文件后?稍后在我的代码中,我使用jdata.iteritems()
来获取所有键和值。
如果我这样写
jdata = json.load(open(json_path))
convert = json.dumps(jdata, {"true": True, "false": False})
它会打印出来:
{"XYZ": true}
但是那我做不到
for key, value in convert.iteritems():
#code
因为我得到这个错误:
AttributeError: 'str' object has no attribute 'iteritems'
我怎样才能解决这个问题?
编辑这是代码
def ReadAndValidate(directory, json_path):
jdata = json.load(open(json_path))
path = findAllFiles(directory)
if os.path.isdir(path):
for root, dirs, files in os.walk(path):
for key, value in jdata.iteritems():
for name in files:
with open(os.path.join(root, name)) as fle:
content = fle.read()
if name.endswith('.txt') and re.search(Wordboundry(key), content):
print "Name", key, "was found in", name, "\n"
writeToFile(value, content, name, key)
else:
print "Name", key, "was not found in", name"
def writeToFile(value, file_content, file, name):
if type(value) is list:
nrOfvalue = ' '.join([str(myValue) for myValue in value])
nrOfvalue = "[ " + nrOfvalue + " ]"
content = regFindName(name, nrOfvalue, file_content)
else:
content = regFindName(name, value, file_content)
writeToFile(file, content)
我正在使用argumentsparser,其中目录可能是“ C \ Mydir \ Dir1”和json_path“ C:\ MyDir \ Dir1 \ myjson.json
myjson包含:
{"XYZ": true}
函数尝试在文件中查找“ XYZ”。Leys说可以在文件“ myFile.txt”中找到它,该文件包含:
XYZ = false;
如果名称匹配,我想将false替换为true。“ myFile.txt”中的预期输出应为:
XYZ = true
regFindName()通过使用regex ++查找“名称”(例如XYZ)
我相信您会误解json.loads
和json.dumps
(及其堂兄json.load
和json.dump
)的角色。
json.dumps
(或json.dump
)将Python对象转换为根据JSON标准设置格式的字符串(或文件)。
json.loads
(或json.load
)将JSON格式的字符串(或文件)转换为相应的Python对象。
考虑以下程序:
import json
orig_python_object = { "XYZ": True }
json_string = json.dumps(orig_python_object)
new_python_object = json.loads(json_string)
assert orig_python_object == new_python_object
assert isinstance(orig_python_object, dict)
assert isinstance(new_python_object, dict)
assert isinstance(json_string, str)
for key, value in new_python_object.iteritems():
pass
当然,不能.iteritems()
得到的结果.dumps()
。的结果.dumps()
是a str
,而不是a dict
。
但是,您可以将其str
(通过文件,套接字或载体鸽)传输到另一个程序,并且该另一个程序可以.loads()
将其转换回Python对象。
返回您的问题:
加载json文件后,如何将“ True”转换为true?
您可以通过将其转换回JSON来实现。
python_object = json.load(open("somefile.json"))
for k,v in python_object.iteritems():
# some code
pass
json_str = json.dumps(python_object)
assert 'true' in json_str
assert 'True' in str(python_object)
编辑 :
您已经在问题中发布了程序的很大一部分,这使问题所在更加明显。如果我是你,我会做两件事之一:
1)我会这样修改writeToFile
:
def writeToFile(value, file_content, file, name):
if type(value) is list:
nrOfvalue = ' '.join([str(myValue) for myValue in value])
nrOfvalue = "[ " + nrOfvalue + " ]"
content = regFindName(name, nrOfvalue, file_content)
elif isinstance(value, bool):
content = regFindName(name, str(value).lower(), file_content)
else:
content = regFindName(name, value, file_content)
writeToFile(file, content)
或者,2)我将这样修改myjson.json
:
{"XYZ": "true"}
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句