This question already has answers here:
How to convert string representation of list to a list
(19 answers)
Closed 5 months ago.
Well, I am using python. And I have case here.
from my api. The following key and value is coming.
games : ["['football','cricket']"]
Now i want to get that football and cricket from coming games value and store in python list.
expected output:
print(games) ==> ["football","circket"]
print(type(games)) ==> <class list>
Perhaps ast.literal_eval function would be useful here. Just pass the string into the function and a list of strings will be returned.
This will be more robust that trying to manipulate the string, as the function is designed to ingest (if you will) key Python data structures as strings; perform a bit of validation to ensure the string is not malicious, and output the associated object.
Docs linked here
Source code linked here
For example:
import ast
games = ["['football','cricket']"]
ast.literal_eval(games[0])
Output:
['football','cricket']
this should work
a = ["['football','cricket']"]
out = [val.strip("'") for val in a[0].strip("[|]").split(",")]
print(out)
['football', 'cricket']
I find it unlikely that a api is sending a non-json string. This string is not json because of the single quotes. If you convert them to double quotes, you have a valid json.
list_games = json.loads(games[0].replace("'",'"'))
Related
This question already has an answer here:
Why does printing a tuple (list, dict, etc.) in Python double the backslashes?
(1 answer)
Closed 7 months ago.
When I create a string containing backslashes, they get duplicated:
NOTE : I want to add \ in request because i want to call third party API and they want me to send request with \ in some of their keys.
I have taken reference from this answer Why do backslashes appear twice?, but its working only for string, not for dict.
mystr = {"str": "why\does\it\happen?"}
print(mystr)
output:
{'str': 'why\\does\\it\\happen?'}
here i am attching a screenshot for better understanding.
mystr isn't a str, it's a dict. When you print a dict, it prints the repr of each string inside it, rather than the string itself.
>>> mydict = {"str": "why\does\it\happen?"}
>>> print(mydict)
{'str': 'why\\does\\it\\happen?'}
>>> print(repr(mydict['str']))
'why\\does\\it\\happen?'
>>> print(mydict['str'])
why\does\it\happen?
Note that the repr() includes elements other than the string contents:
The quotes around it (indicating that it's a string)
The contents use backslash-escapes to disambiguate the individual characters. This extends to other "special" characters as well; for example, if this were a multiline string, the repr would show linebreaks as \n within a single line of text. Actual backslash characters are always rendered as \\ so that they can be distinguished from backslashes that are part of other escape sequences.
The key thing to understand is that these extra elements are just the way that the dict is rendered when it is printed. The actual contents of the string inside the dict do not have "doubled backslashes", as you can see when you print mydict['str'].
If you are using this dict to call an API, you should not be using str(mydict) or anything similar; if it's a Python API, you should be able to use mydict itself, and if it's a web API, it should be using something like JSON encoding (json.dumps(mydict)).
I think that to build the printed string of the dict, python call the __repr__ method of object inside it (for the values) instead of the __str__ as you would expect for printing the dict.
It would make sense since dict can contain every type of object not just string so the __repr__| method can be found everywhere (it's included in the base object in python) when the __str__ need to be written.
But it's only a guess, not a definitive answer.
This question already has answers here:
converting a string to a list of tuples
(4 answers)
Closed 1 year ago.
Can someone help me? I want to transform this string
"[(14577003, 0.05889), (14066001, 0.04027)]"
to
[(14577003, 0.05889), (14066001, 0.04027)]
by removing the quotes. How can I do that in Python ?
Thank you very much for your response.
you can eval the content of the string, so you get the content as a list:
ll = eval("[(14577003, 0.05889), (14066001, 0.04027)]")
ll
output is:
[(14577003, 0.05889), (14066001, 0.04027)]
You also have a safer way to do it:
import ast
ll = ast.literal_eval("[(14577003, 0.05889), (14066001, 0.04027)]")
ll
The ast.literal_eval() is a a safe eval() that only evaluates literals such as strings, bytes, numbers, tuples, lists, dicts, sets, booleans, None and Ellipsis. This can be used for safely evaluating strings containing Python values from untrusted sources without the need to parse the values oneself.
See http://docs.python.org/dev/library/ast.html#ast.literal_eval
This question already has answers here:
How to convert string representation of list to a list
(19 answers)
Closed 1 year ago.
I have a column in a Pandas dataframe that contains dictionaries with Python native types in it (like strings, integers, floats etc.).
After saving this dataframe in a csv file and reloading it, the values are loaded as strings and it seems to be impossible to cast them back to dictionaries. The Python string representation of a dictionary is not a JSON (single quotes instead of double quotes and boolean starts with a capital letter instead of a lower case letter) so it is not possible to use json.loads()
Consider this example :
import json
import pandas as pd
df = pd.DataFrame({'dict_column' : [{'key' : 'val'}]})
df.iloc[0, 0]['key'] --> returns 'val'
df.to_csv('file.csv', index=False)
df_loaded = pd.read_csv('file.csv')
df_loaded.iloc[0, 0]['key'] --> returns an error
json.loads(df_loaded.iloc[0, 0]) --> returns an error
Because of this, it seems very important to apply json.dumps on a dict column before saving a dataframe in a csv. But how to do for existing CSVs ? Is there anyway to cast a string representation of a dict back to a dict ? I have a huge dataframe saved like that and I don't want to lose the data in it.
I hope that there is a better solution than replacing the single quotes to double quotes in the CSV file directly.
Try:
df_loaded["dict_column"] = df_loaded["dict_column"].apply(eval)
df_loaded.iloc[0, 0]['key'] # Now returns 'val'
Let's say I want to create a json object following the structure:
{"favorite_food":["icecream","hamburguers"]}
to do so in python, if i know the whole string in advance, I can just do:
json.dumps({"favorite_food":["icecream","hamburguers"]})
which works fine.
my question though is, how would i do the same thing if i wanted to get the object as a result of a string interpolation? For example:
favorite food = 'pizza'
json.dumps({"favorite_food":[%s]}) %favorite_food
the issue i found is, if I do the interpolation prior to calling the json.dumps:
dict= '{"favorite_food":[%s]}' % favorite_food
if i then do json.dumps(dict) , because of the string quotation, the json_dumps returns:
{"favorite_food":[pizza]}
that is, is not a dict anymore (but a string with the structure of a dict)
How can i solve this simple issue?
Why not just:
>>> food = "pizza"
>>> json.dumps({"favorite_food":[food]})
'{"favorite_food": ["pizza"]}'
json,dumps takes actual values as input --- that is, real dicts, lists, ints, and strings. If you want to put your string value in the dict, just put it in. You don't want to put in a string representation of it, you want to put in the actual value and let json.dumps make the string representation.
How about below:
favorite_food = 'pizza'
my_dict = {"favorite_food":[favorite_food]}
print json.dumps(my_dict)
I found this is very simple.
Ok, so I'm using an API. I'm trying to display a list that is returned by the api. The challenge is that I need to use .json to go through the response, but then it makes the list a json list and looks wrong.
checkList #is the return value
>>> checkList
u'{"list":["ad","ae"]}'
>>> str(checkList.json()['list'])
"[u'ad', u'ae']"
I'm using a python shell. How would I remove the " u' " from each element in the list? Thanks
The issue is not really in removing the u from the start of those strings. The easiest way to do this is to import the json module and call json.dumps(checklist.json()['list']). It will do the right thing for you. The strings the json module returns are unicode objects (and are represented in the repr) as unicode literals. To "remove" them you need to handle the unicode strings better and this is the easiest way that will result in the least hair pulling and most forward compatibility with python 3.