I'm processing data within Python, and would like to stream records to R using JSON formatting and simplejson
on the Python side, and rjson
on the R
side.
How can I output records out of Python so that R's fromJSON
can process them into a one-line dataframe? Thanks
try:
import simplejson as json
except ImportError:
import json
record = {'x':1,'y':1}
print json.dumps( record )
Result:
{"y": 1, "x": 1}
However, I'd need the result to be "{\\"x\\":1,\\"y\\":2}"
, as R
needs that formatting to use the data:
library(rjson)
as.data.frame( fromJSON( "{\"x\":1,\"y\":2}" ) )
x y
1 1 2
Thanks.
Two options:
(1) If your JSON does not contain both single & double quotes, wrap the entire JSON in the quote type not being used.
(2) If you need to escape the quotes (ie, because both are in your JSON), then you need to escape the escape character. That is, use double slashes:
\\\\"y\\\\"
(note that the second point applies to any string in R that has needs an escape character)
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.