È possibile effettuare una chiamata di sistema? È possibile utilizzare uno script Python come questo per convertire i dati serializzati in JSON:
# pickle2json.py
import sys, optparse, cPickle, os
try:
import json
except:
import simplejson as json
# Setup the arguments this script can accept from the command line
parser = optparse.OptionParser()
parser.add_option('-p','--pickled_data_path',dest="pickled_data_path",type="string",help="Path to the file containing pickled data.")
parser.add_option('-j','--json_data_path',dest="json_data_path",type="string",help="Path to where the json data should be saved.")
opts,args=parser.parse_args()
# Load in the pickled data from either a file or the standard input stream
if opts.pickled_data_path:
unpickled_data = cPickle.loads(open(opts.pickled_data_path).read())
else:
unpickled_data = cPickle.loads(sys.stdin.read())
# Output the json version of the data either to another file or to the standard output
if opts.json_data_path:
open(opts.json_data_path, 'w').write(json.dumps(unpickled_data))
else:
print json.dumps(unpickled_data)
In questo modo, se il vostro ottenere i dati da un file si potrebbe fare qualcosa di simile:
<?php
exec("python pickle2json.py -p pickled_data.txt", $json_data = array());
?>
o se si vuole salvarlo in un file questo:
<?php
system("python pickle2json.py -p pickled_data.txt -j p_to_j.json");
?>
Tutto il codice di cui sopra, probabilmente non è perfetto (io non sono uno sviluppatore PHP), ma sarebbe qualcosa di simile a questo lavoro per voi?
fonte
2010-06-14 21:56:11
Hai menzionato che la serializzazione non è sotto il tuo controllo. Come stai ricevendo questi dati in salamoia? Un file locale? –
Viene dal database di Django, è session_data nella tabella delle sessioni di Django. Probabilmente finirò per scrivere SessionMiddleware per serializzare il session_data come JSON. Ho solo desiderato di non dover modificare l'app di Django per questo. – Ciantic