Get value from string python [duplicate] - python

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

Related

How to convert string into list? [duplicate]

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("'",'"'))

Why do backslashes appear twice in dict? [duplicate]

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.

How to cast Python string representation of a dict back to a dict [duplicate]

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'

Add double quotes string to return JSON [duplicate]

This question already has answers here:
How to convert raw javascript object to a dictionary?
(6 answers)
Closed 1 year ago.
Currently I'm parsing a string using split and returning the below
{fruit1:"Apple",fruit2:"Orange",fruit3:"Pear"}
I want to convert this string to json using json.loads but I get the below error
json.decoder.JSONDecodeError: Expecting property name enclosed in double quotes
How would I go about adding double quotes to this string so I can perform the desired result below
food = '{"fruit1":"Apple","fruit2":"Orange","fruit3":"Pear"}'
print(food['fruit1'])
-----------
Apple
At least in this case, you can treat the string as a jq filter, and use the Python bindings for jq (a third-party package, not in the standard library) to evaluate it to a dict object.
>>> import jq
>>> d = jq.first('{fruit1:"Apple",fruit2:"Orange",fruit3:"Pear"}', None)
>>> d['fruit1']
'Apple'

How to convert a list of unicode strings to regular strings [duplicate]

This question already has answers here:
Removing u in list
(8 answers)
Closed 6 years ago.
I want to remove 'u' from every element in the list, can anybody help me?
[u'four', u'gag', u'prefix', u'woods']
The issue is with the encoding of strings.
Do this :
l = [u'four', u'gag', u'prefix', u'woods']
l2 = [i.encode('UTF-8') for i in l]
print l2
['four', 'gag', 'prefix', 'woods']
The u is an attribute that tells what type of string it is. If it was a byte string, this would be b. If you call type on these, they will return String. The difference between Unicode and something like ASCII is that Unicode is a super-set of ASCII that is the same for 0-127, but has more capability to represent different types of characters. These can be UTF-8 or UTF-32 or whatever, but generally are larger than one byte.
It should behave the same for 99% of the things that you want to do, but you can also change the encoding if you have a function that needs a very particular type of string.

Categories