PHP多次元配列をPython辞書形式の文字列に変換するには?PHP配列をPython辞書形式の文字列に変換
var_dump($myarray);
array(2) { ["a1"]=> array(2) { ["29b"]=> string(0) "" ["29a"]=> string(0) "" } ["a2"]=> array(2) { ["29b"]=> string(0) "" ["29a"]=> string(0) "" } }
PHP多次元配列をPython辞書形式の文字列に変換するには?PHP配列をPython辞書形式の文字列に変換
var_dump($myarray);
array(2) { ["a1"]=> array(2) { ["29b"]=> string(0) "" ["29a"]=> string(0) "" } ["a2"]=> array(2) { ["29b"]=> string(0) "" ["29a"]=> string(0) "" } }
テキスト経由でPythonの辞書にPHPの連想配列に変換する必要がある場合は、両方の言語がそれを理解しているので(あなたは、Pythonのためのsimplejsonのようなものをインストールする必要がありますが)、JSONを使用することもできます。
http://www.php.net/manual/en/function.json-encode.php http://simplejson.readthedocs.org/en/latest/index.html
例(明らかにこれは自動的に行われるためのいくつかの作業が必要になります)...
<?php
$arr = array('test' => 1, 'ing' => 2, 'curveball' => array(1, 2, 3=>4));
echo json_encode($arr);
?>
# elsewhere, in Python...
import simplejson
print simplejson.loads('{"test":1,"ing":2,"curveball":{"0":1,"1":2,"3":4}}')
Python 2+ [' json'ライブラリが組み込まれています(https:// docs.python.org/2/library/json.html)。 – kungphu
あなたはjson_encode()
を使って、欲しいものを達成する必要があります。 Pythonの表記は、このように、それはあなたのニーズを満たす必要があり、非常によく似ています。
echo json_encode($myarray);
あなたの配列はPythonで、このようなものでなければなりません:
my_array = {
'a1': {
'29b': '',
'29a': ''
},
'a2': {
'29b': '',
'29a': ''
}
}
あなたが期待どおりに動作しますか?
ここで上記kungphuさんのコメントに基づいて、私の解決策とFastest way to convert a dict's keys & values from `unicode` to `str`?
import collections, json
def convert(data):
if isinstance(data, unicode):
return str(data)
elif isinstance(data, collections.Mapping):
return dict(map(convert, data.iteritems()))
elif isinstance(data, collections.Iterable):
return type(data)(map(convert, data))
else:
return data
import json
DATA = json.loads('{"test":1,"ing":2,"curveball":{"0":1,"1":2,"3":4}}')
print convert(DATA)
でRichieHindleの答えは、だから、あなたはそれがマルチのpythonであるかのようにフォーマットされた文字列としてPHP多次元配列をプリントアウトすることを意味しています次元配列? –
はい、私は配列をpythonスクリプトに渡して、さらなる解析を行いたいと思います。 Pythonが 'sys.argv'を介して受け入れるためには、それを文字列としてフォーマットする必要があります – user602599