Convert a String Representation of a Dictionary to a Dictionary

Convert a String representation of a Dictionary to a dictionary

You can use the built-in ast.literal_eval:

>>> import ast
>>> ast.literal_eval("{'muffin' : 'lolz', 'foo' : 'kitty'}")
{'muffin': 'lolz', 'foo': 'kitty'}

This is safer than using eval. As its own docs say:


>>> help(ast.literal_eval)
Help on function literal_eval in module ast:

literal_eval(node_or_string)
Safely evaluate an expression node or a string containing a Python
expression. The string or node provided may only consist of the following
Python literal structures: strings, numbers, tuples, lists, dicts, booleans,
and None.

For example:

>>> eval("shutil.rmtree('mongo')")
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "<string>", line 1, in <module>
File "/opt/Python-2.6.1/lib/python2.6/shutil.py", line 208, in rmtree
onerror(os.listdir, path, sys.exc_info())
File "/opt/Python-2.6.1/lib/python2.6/shutil.py", line 206, in rmtree
names = os.listdir(path)
OSError: [Errno 2] No such file or directory: 'mongo'
>>> ast.literal_eval("shutil.rmtree('mongo')")
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "/opt/Python-2.6.1/lib/python2.6/ast.py", line 68, in literal_eval
return _convert(node_or_string)
File "/opt/Python-2.6.1/lib/python2.6/ast.py", line 67, in _convert
raise ValueError('malformed string')
ValueError: malformed string

Convert string representation of dictionary to dictionary

Here is a way to do it that works on your example, but would fail if things were more nested or whatever. In this case, I would rather recommend trying to get the data in a more manageable format, like JSON, rather than try to write a parser that would work in any possible case.

It's a rather crude way of parsing the string by splitting it with regex:

s = "{1:p→q,2: ¬q,3: ¬ (¬p),4: ¬p,5:[edge(¬p,¬p∧ ¬ (¬p)),edge(¬ (¬p),¬p∧ ¬ (¬p)),rule('∧I')],6:[edge(¬p,p),edge(¬p∧ ¬ (¬p),p),rule('¬E')],7:[edge(p,q),edge(p→q,q),rule('→E')],8:[edge(q,q∧ ¬q),edge(¬q,q∧ ¬q),rule('∧I')],9:[edge(¬ (¬p),¬p),edge(q∧ ¬q,¬p),rule('¬E')]}"

import re

# We split on optional ",", a number and ":"
split = re.split(r',?(\d+):', s[1:-1])
# ['', '1', 'p→q', '2', ' ¬q', '3', ' ¬ (¬p)' ...]

# Convert the numbers to int
split[1::2] = [int(n) for n in split[1::2]]

# We create a dict with the numbers as keys
d = dict(zip(split[1::2], split[2::2]))

# The values starting with "[" need to be converted to lists
for k, v in d.items():
if v.startswith('['):
# We split on "edge" or "rule"
split = re.split(',?(edge|rule)',v[1:-1])
# and create the list, joining each separator with what follows it
d[k] = list(''.join(parts) for parts in zip(split[1::2], split[2::2]))

print(d)

Output:

{1: 'p→q',
2: ' ¬q',
3: ' ¬ (¬p)',
4: ' ¬p',
5: ['edge(¬p,¬p∧ ¬ (¬p))', 'edge(¬ (¬p),¬p∧ ¬ (¬p))', "rule('∧I')"],
6: ['edge(¬p,p)', 'edge(¬p∧ ¬ (¬p),p)', "rule('¬E')"],
7: ['edge(p,q)', 'edge(p→q,q)', "rule('→E')"],
8: ['edge(q,q∧ ¬q)', 'edge(¬q,q∧ ¬q)', "rule('∧I')"],
9: ['edge(¬ (¬p),¬p)', 'edge(q∧ ¬q,¬p)', "rule('¬E')"]}

How to Convert String Dictionary to Dictionary and the split into separate columns

You can use literal_eval() to get the dict object out of string. After that get the items using items() method on the dictionary. Lastly, construct the dataframe.

import ast
import pandas as pd

d = [[0, "{'Owl': '8109284', 'county': '27'}"],
[1, "{'Kid': '298049', 'county': '28'}"],
[2, "{'Tree': '190849', 'county': '29'}"],
[3, "{'Garden': '9801294', 'county': '30'}"],
[4, "{'House': '108094', 'county': '31'}"]]

df = pd.DataFrame(d, columns=['a', 'b'])


pd.DataFrame(df['b'].apply(lambda x: [j for i in ast.literal_eval(x).items() for j in i]).to_list(),
columns=['Item', 'Count', 'County', 'County Number'])
        Item    Count   County  County Number
0 Owl 8109284 county 27
1 Kid 298049 county 28
2 Tree 190849 county 29
3 Garden 9801294 county 30
4 House 108094 county 31

How to cast Python string representation of a dict back to a dict

Try:

df_loaded["dict_column"] = df_loaded["dict_column"].apply(eval)
df_loaded.iloc[0, 0]['key'] # Now returns 'val'

Best way to convert dictionary in string and remove quotes ( or ') around values

str_ = "{" + ", ".join([f"{k}: {v}" for k, v in Test.items()]) + "}"

Unable to convert string representation of dictionary to dictionary

They're not JSON, but Python dictionary literals.

You can use ast.literal_eval to convert literals to python object.

import ast

with open('file.txt') as f:
for line in f:
if 'name' in line:
d = ast.literal_eval(line)
print(type(d))

Convert string representation of a Dictionary to a pandas data frame

You can use this example to extract the values:

import requests
import pandas as pd
from bs4 import BeautifulSoup

url = "https://www.ssga.com/de/de/institutional/etfs/funds/spdr-msci-acwi-imi-ucits-etf-spyi-gy"

acwi = BeautifulSoup(requests.get(url).content, "lxml")
data = acwi.find("input", {"id": "fund-geographical-breakdown"}).get("value")

df = pd.read_json(data)["attrArray"].apply(pd.Series)
df["name"] = df["name"].str["value"]
df["weight"] = df["weight"].str["value"]
print(df)

Prints:

                     name  weight
0 USA 59,30%
1 Japan 5,86%
2 Großbritannien 4,19%
3 China 3,32%
4 Kanada 3,26%
5 Frankreich 2,72%
6 Schweiz 2,71%
7 Deutschland 2,17%
8 Australien 2,11%

...


Related Topics



Leave a reply



Submit