为什么 json.loads() 返回一个字符串?这是我的代码:
import json
d = """{
"reference": "123432",
"business_date": "2019-06-18",
"final_price": 40,
"products": [
{
"quantity": 4,
"original_price": 10,
"final_price": 40,
}
]
}"""
j = json.loads(json.dumps(d))
print(type(j))
Run Code Online (Sandbox Code Playgroud)
输出:
<class 'str'>
Run Code Online (Sandbox Code Playgroud)
它不应该返回一个 json 对象吗?这里需要什么改变?
是否有更多的Pythonic方法来实现此逻辑:
res = func()
if res is not None: # res is either True, False, something else
if res is True:
#do something # res is True
elif res is False:
#do something else # res is False
else:
#do something else2 # res is not in [True,False]
else:
#failed # res is None
Run Code Online (Sandbox Code Playgroud) 我有一个字典d,我想修改键并创建一个新字典。最好的方法是什么?
这是我现有的代码:
import json
d = json.loads("""{
"reference": "DEMODEVB02C120001",
"business_date": "2019-06-18",
"final_price": 40,
"products": [
{
"quantity": 4,
"original_price": 10,
"final_price": 40,
"id": "123"
}
]
}""")
d2 ={
'VAR_Reference':d['reference'],
'VAR_date': d['business_date'],
'VAR_TotalPrice': d['final_price']
}
Run Code Online (Sandbox Code Playgroud)
有没有更好的方法可以使用另一个映射字典或可以保留映射值的文件来映射值。
例如:
d3 = {
'reference':'VAR_Reference',
'business_date': 'VAR_date',
'final_price': 'VAR_TotalPrice'
}
Run Code Online (Sandbox Code Playgroud)
感谢任何提示或提示。
我尝试了 PyInstaller,但无法在包中包含配置和 .txt 文件。
cx_freeze 在安装过程中抛出错误,Python 3.6 及更高版本不支持 py2exe。
我可以将哪个 Python-to-EXE 库用于 Python 3.7?