我正在處理一些作爲Ruby哈希字符串返回的命令輸出。 (來自所謂的mcollective)。將Ruby哈希字符串轉換爲Python字典
這是我收到的示例串:
{:changes=>{"total"=>0}, :events=>{"failure"=>0, "success"=>0, "total"=>0}, :version=> {"puppet"=>"2.7.21 (Puppet Enterprise 2.8.1)", "config"=>1381497648}, :time=> {"filebucket"=>0.000287, "cron"=>0.00212, "package"=>0.398982, "exec"=>0.001314, "config_retrieval"=>5.60761618614197, "anchor"=>0.001157, "service"=>0.774906, "total"=>9.85111718614197, "host"=>0.002662, "user"=>0.063606, "file"=>2.998467, "last_run"=>1381497660}, :resources=> {"skipped"=>6, "failed_to_restart"=>0, "out_of_sync"=>0, "failed"=>0, "total"=>112, "restarted"=>0, "scheduled"=>0, "changed"=>0}}
我能夠寫這個小解析器,但是這將是一個繁瑣的任務。有沒有人知道一個庫或代碼片段,可以將它轉換成Python字典?
如果您認爲我應該解析它,歡迎提供任何提示。
一個簡單的黑客就是用= 1替換=:[a-z] +,然後調用ast.literal_eval或json.loads。這是不正確的,因爲它忽略了字符串文字,但現在可能已經足夠好了。 – Antimony
根據他們的文檔http://docs.puppetlabs.com/mcollective/reference/basic/basic_cli_usage.html他們也可以導出JSON:'-j'標誌。 – georg
不幸的是,版本即時運行。但是,這將是最好的 – joeButler