Come straight to the point.
A str object below:
s = '{"key1":"value1", "key2":"value2", "key3":"value3"}'
As you see, dict is wrapped in str. Now how to escape dict from str?
In other words, is it possible d = operate(s), d["key1"] = "value1", and etc?
>>> ast.literal_eval('{"key1":"value1", "key2":"value2", "key3":"value3"}')
{'key3': 'value3', 'key2': 'value2', 'key1': 'value1'}
i'd use json:
try:
import json
except ImportError:
import simplejson as json
d = json.loads(s)
You're looking for eval.
Related
there's a json obj:
{'key1': 'val1', 'key2': "{"key3": ["val3"]}"}
how to parse json string and json obj in the same json obj
json.load() =>
AttributeError: 'str' object has no attribute 'read'
json.loads() =>
JSONDecodeError: Expecting ',' delimiter: line 1 column 232 (char 231)
Pretty sure you want to use JSON library as Himanshu suggested.
I think this will answer your questions: https://bogotobogo.com/python/python-json-dumps-loads-file-read-write.php
the json dumps method takes in an object and returns a string:
a = {'foo': 3}
json.dumps(a)
>>> '{"foo": 3}'
the json load method takes in a file-like object, reads the data from that object, and uses that string to create an object:
with open('file.json') as fh:
a = json.load(fh)
For your specific JSON case I think you want it reformatted like so:
import json
j = {'key1': 'val1',
'key2': "{'key3': ['val3']}"
}
print(json.dumps(j))
>>>> {"key1": "val1", "key2": "{'key3': ['val3']}"}
There are four methods in the json libary: load, loads, dumps, dumps. Here is what they do:
Easiest way is to format the dict with multi-line string, then use json.dumps:
d = {'key1': 'val1', 'key2': """{"key3": ["val3"]}"""}
json.dumps(d) # a new json string
json.loads(d["key2"]) # the obj
I need a little help processing a String to a Dict, considering that the String is not in a common format, but an output from a UDF function
The return from the PySpark UDF looks like the string below:
"{list=[{a=1}, {a=2}, {a=3}]}"
And I need to convert it to a python dictionary with the structure below:
{
"list": [
{"a": 1}
{"a": 2}
{"a": 3}
]
}
So I can access it's values, like
dict["list"][1]["a"]
I already tried using:
JSON.loads
ast_eval()
Could someone please help me?
As an example of how this unparsed string is generated:
#udf()
def execute_method():
return {"list": [{"a":1},{"b":1}{"c":1}]}
df_result = df_source.withColumn("result", execute_method())
By the very least you will need to replace = with : and surround keys with double quotes:
import json
import re
string = "{list=[{a=1}, {a=2}, {a=3}]}"
fixed_string = re.sub(r'(\w+)=', r'"\1":', string)
print(type(fixed_string), fixed_string)
parsed = json.loads(fixed_string)
print(type(parsed), parsed)
outputs
<class 'str'> {"list":[{"a":1}, {"a":2}, {"a":3}]}
<class 'dict'> {'list': [{'a': 1}, {'a': 2}, {'a': 3}]}
try this :
import re
import json
data="{list=[{a=1}, {a=2}, {a=3}]}"
data=data.replace('=',':')
pattern=[e.group() for e in re.finditer('[a-z]+', data, flags=re.IGNORECASE)]
for e in set(pattern):
data=data.replace(e,"\""+e+"\"")
print(json.loads(data))
I'm trying to load yaml that contains python formatted strings, e.g. test: {formatted_string}. This would allow me to format the string using dictionary["test"].format(formatted_string="hello yaml"), but when I load the yaml, it's automatically converted to {'test': {'formatted_string': None}} instead of {'test': '{formatted_string}'}.
There are dozens of .yaml files that are already formatted in this way.
I don't see this in the pyyaml docs or anywhere on SO.
Code in full for clarity:
import yaml
data = """
test: {formatted_string}
"""
d1 = yaml.load(data)
print(d1)
# {'test': {'formatted_string': None}}
d2 = {"test": "{formatted_string}"}
print(d2)
# {'test': '{formatted_string}'}
d2["test"] = d2["test"].format(formatted_string="hello yaml")
print(d2)
# {'test': 'hello yaml'}
Thanks!
The { character in YAML (as in JSON) introduces a dictionary. That is this:
a_dictionary:
key1: value1
key2: value2
Is completely equivalent to:
a_dictionary: {key1: value1, key2: value2}
So when you write...
test: {formatted_string}
...the YAML parser things you are introducing a dictionary, and that it has a single key (formatted_string) and no value. If you want to use a { as part of a YAML value you need to quote it:
test: "{formatted_string}"
Compare:
>>> yaml.safe_load('test: {formatted_string}')
{'test': {'formatted_string': None}}
>>> yaml.safe_load('test: "{formatted_string}"')
{'test': '{formatted_string}'}
In general, if you always quote your YAML strings your life will be easier :).
A really basic requirement.
I would like to convert from this format:
"column1=value1;column2=value2"
to this format (JSON):
{"column1":"value1","column2":"value2"}
Any best approach in Python would be appreciated.
Thanks in advance.
using regular expressions
import re
REGEX = r"([^=;]+)=([^=;]+)"
finder = re.compile(REGEX)
s = "column1=value1;column2=value2"
matches = re.finditer(finder, s)
d = {}
for match in matches:
key = match.group(1)
val = match.group(2)
d[key] = val
print(d)
Output:
{'column2': 'value2', 'column1': 'value1'}
If you really want to parse your string to JSON you should try something like this:
import json # simplejson if you use a python version below 2.6
string = u'{"column1":"value1", "column2": "value2"}'
json = json.loads(string)
If you want to parse your string to a dictionary you should try ast:
import ast
string = u'{"column1":"value1", "column2": "value2"}'
ast.literal_eval(string)=>{'column1': 'value1', 'column2': 'value2'}
I want to convert such query string:
a=1&b=2
to json string
{"a":1, "b":2}
Any existing solution?
Python 3+
import json
from urllib.parse import parse_qs
json.dumps(parse_qs("a=1&b=2"))
Python 2:
import json
from urlparse import parse_qs
json.dumps(parse_qs("a=1&b=2"))
In both cases the result is
'{"a": ["1"], "b": ["2"]}'
This is actually better than your {"a":1, "b":2}, because URL query strings can legally contain the same key multiple times, i.e. multiple values per key.
>>> strs="a=1&b=2"
>>> {x.split('=')[0]:int(x.split('=')[1]) for x in strs.split("&")}
{'a': 1, 'b': 2}
Python 3.x
from json import dumps
from urllib.parse import parse_qs
dumps(parse_qs("a=1&b=2"))
yelds
{"b": ["2"], "a": ["1"]}
dict((itm.split('=')[0],itm.split('=')[1]) for itm in qstring.split('&'))