我有一个使用python的项目,我想将PHP转换为python.我在PHP的数组中混淆了将它转换为python ...
在PHP的旧代码中...它看起来像这样,
array(
"Code" => 122,
"Reference" => 1311,
"Type" => 'NT',
"Amount" => 100.00
);
Run Code Online (Sandbox Code Playgroud)
这就是我将它转换为python所做的...
dict = {
"Code":122,
"Reference":1311,
"Type":'NT',
"Amount":100.00
}
Run Code Online (Sandbox Code Playgroud)
是我的转换PHP到python是正确的?
Ray*_*ger 11
你的转换基本上是正确的(虽然我不会使用dict作为变量名,因为它掩盖了同名的内置类构造函数).话虽这么说,PHP数组是有序映射,所以你应该使用Python OrderedDict而不是常规字典,以便保持插入顺序:
>>> import collections
>>> od = collections.OrderedDict([
('Code', 122),
('Reference', 1311),
('Type', 'NT'),
('Amount', 100.00),
])
>>> print od['Amount']
100.0
>>> od.keys()
['Code', 'Reference', 'Type', 'Amount']
Run Code Online (Sandbox Code Playgroud)