python string to list (in string and json) [duplicate] - python

This question already has answers here:
How can I parse (read) and use JSON?
(5 answers)
Closed last year.
I want to convert these strings to a list. in python
from
42["pause",{"all":false,"media_only":false,"media_exclude":false}]
(type : string)
to["pause",{"all":false,"media_only":false,"media_exclude":false}]
(type : list (in string and json))
There is a way to parse the string directly, but since the types of incoming data are various, I was looking for a function that can be converted directly, but I couldn't find it. How can I do that?

Can you try the following:
import json
text = '''42["pause",{"all":false,"media_only":false,"media_exclude":false}]'''
text = text[text.find('['):]
result = json.loads(text)
print(result)
Output:
['pause', {'all': False, 'media_only': False, 'media_exclude': False}]

Related

Python - turn string of list of dictionaries to list of dictionaries [duplicate]

This question already has answers here:
How to convert string representation of list to a list
(19 answers)
Closed 8 months ago.
I have a string that looks like this:
'{"screen_name":"Brian","avatar":1},{"screen_name":"David","avatar":21},{"screen_name":"Teo","avatar":34}'
How can I make it a list of dictionaries?
I tried with json.loads, but it threw an error...
before trying json.loads, you have to make sure that is surrounded by square brackets.
You can achieve that by using format or fstrings, so you can do:
>>> import json
>>> json.loads(f"[{original}]")
[{"screen_name":"Brian","avatar":1},{"screen_name":"David","avatar":21},{"screen_name":"Teo","avatar":34}]

How to convert a string to a nested list? [duplicate]

This question already has answers here:
Convert a String representation of a Dictionary to a dictionary
(11 answers)
Convert JSON string to dict using Python [duplicate]
(4 answers)
Closed 2 years ago.
I'm trying to convert a string into a nested list so that I can select elements based on the date. This is the string I'm trying to convert:
{"data":[[[""],["2,681,118"]],[[""],["529,106"]],[[""],["1402"]],[[""],["33"]],[["","Positivas","Total Px Ag"],["30/07/2020","216","811"],["31/07/2020","316","1,176"],["01/08/2020","383","1,461"],["02/08/2020","529","2,153"],["03/08/2020","635","2,582"],["04/08/2020","1060","3,859"],["05/08/2020","1798","6,143"],["06/08/2020","2597","8,947"],["07/08/2020","3504","12,063"],["08/08/2020","3940","13,713"],["09/08/2020","4869","16,480"],["10/08/2020","5489","18,279"],["11/08/2020","6557","21,555"],["12/08/2020","8,028","28,212"],["13/08/2020","9,190","32,216"],["14/08/2020","10,477","37,026"],["15/08/2020","11,672","41,619"],["16/08/2020","13483","47,399"],["17/08/2020","13,904","49,031"],["18/08/2020","14,886","52,631"],["19/08/2020","17,264","59,726"],["20/08/2020","18,889","65,451"],["21/08/2020","19,958","69,605"],["22/08/2020","21,827","76,373"],["23/08/2020","22,126","77,525"],["24/08/2020","23,096","81,031"],["25/08/2020","24,854","87,281"],["26/08/2020","26,256","93,182"],["27/08/2020","28,024","100,223"],["28/08/2020","29,657","107,145"],["29/08/2020","30,815","112,531"],["30/08/2020","31,527","115,888"],["31/08/2020","32,923","121,165"],["01/09/2020","34,714","129,405"],["02/09/2020","33499","127,851"],["03/09/2020","35944","138,124"],["04/09/2020","37,387","145,603"],["05/09/2020","38,945","153,238"],["06/09/2020","40,005","157,962"],["07/09/2020","41,770","166,722"],["08/09/2020","44,200","177,432"],["09/09/2020","45,646","185,245"],["10/09/2020","47,578","194,561"],["11/09/2020","49,331","203,685"],["12/09/2020","50,739","211,517"],["13/09/2020","51,393","216,127"],["14/09/2020","53,298","223,942"],["15/09/2020","52,289","219,189"],["16/09/2020","53,852","228,420"],["17/09/2020","59,212","254,698"],["18/09/2020","61,373","266,762"],["19/09/2020","63,192","276,724"],["20/09/2020","63,730","281,095"],["21/09/2020","65,454","288,294"],["22/09/2020","67,645","300,123"],["23/09/2020","70,510","313,522"],["24/09/2020","72,931","326,878"],["25/09/2020","74,790","338,618"],["26/09/2020","76,636","350,286"],["27/09/2020","78,231","359,259"],["28/09/2020","79,437","365,650"],["29/09/2020","81,885","378,445"],["30/09/2020","83,536","390,591"],["01/10/2020","86,581","405,469"],["02/10/2020","88,769","419,606"],["03/10/2020","91,497","435,562"],["04/10/2020","92,084","440,129"],["05/10/2020","94,756","452,541"],["06/10/2020","97,219","466,283"],["07/10/2020","99,131","478,478"],["08/10/2020","102,944","497,369"],["09/10/2020","105,924","514,140"],["10/10/2020","108,272","529,318"],["11/10/2020","109,143","534,885"],["12/10/2020","110,292","541,041"],["13/10/2020","113,088","553,014"],["14/10/2020","115,871","568,193"],["15/10/2020","119,985","589,861"],["16/10/2020","122,887","607,628"],["17/10/2020","125,662","622,663"],["18/10/2020","126,524","628,207"],["19/10/2020","128,042","634,754"],["20/10/2020","132,948","659,212"],["21/10/2020","134,937","671,911"],["22/10/2020","139,006","692,615"],["23/10/2020","141,844","709,412"],["24/10/2020","143,776","723,858"],["25/10/2020","144,928","730,451"],["26/10/2020","147,171","741,252"],["27/10/2020","151,468","763,433"],["28/10/2020","154,747","782,588"],["29/10/2020","159302","808,677"],["30/10/2020","161,072","821,999"],["31/10/2020","165,523","846,676"],["01/11/2020","166,657","854,868"],["02/11/2020","167,767","860,221"],["03/11/2020","170,233","872,346"],["04/11/2020","173,729","889,779"],["05/11/2020","176,857","907,448"],["06/11/2020","179,743","934,226"],["07/11/2020","183,321","955,354"],["08/11/2020","184,537","962,916"],["09/11/2020","187,051","975,475"],["10/11/2020","191,016","997,206"],["11/11/2020","193,963","1,015,733"],["12/11/2020","197,760","1,037,854"],["13/11/2020","201,044","1,060,786"],["14/11/2020","203,525","1,078,140"],["15/11/2020","204356","1,083,983"],["16/11/2020","205,248","1,088,192"],["17/11/2020","207,495","1,101,771"],["18/11/2020","211,224","1,122,018"],["19/11/2020","214,894","1,144,216"],["20/11/2020","218,099","1,164,807"],["21/11/2020","221,435","1,193,479"],["22/11/2020","222,343","1,199,903"],["23/11/2020","224,953","1,213,335"],["24/11/2020","228,343","1,232,205"],["25/11/2020","232,075","1,255,370"],["26/11/2020","236,580","1,282,888"],["27/11/2020","238,742","1,301,977"],["28/11/2020","241,835","1,322,140"],["29/11/2020","243,964","1,333,756"],["30/11/2020","245,597","1,343,739"],["01/12/2020","248,976","1,364,088"],["02/12/2020","251,596","1,384,223"],["03/12/2020","255,977","1,409,778"],["04/12/2020","260,859","1,435,230"],["05/12/2020","264,292","1,456,665"],["06/12/2020","265,698","1,466,555"],["07/12/2020","268,132","1,480,322"],["08/12/2020","271,505","1,499,624"],["09/12/2020","273,014","1,508,962"],["10/12/2020","277,248","1,532,527"],["11/12/2020","280,562","1,553,248"],["12/12/2020","286,577","1,582,159"],["13/12/2020","288,254","1,591,666"],["14/12/2020","293,324","1,615,371"],["15/12/2020","299,386","1,647,621"],["16/12/2020","304,702","1,677,361"],["17/12/2020","309,972","1,703,326"],["18/12/2020","316,089","1,731,954"],["19/12/2020","320,504","1,756,153"],["20/12/2020","322,565","1,768,605"],["21/12/2020","326,604","1,787,468"],["22/12/2020","332,330","1,814,071"],["23/12/2020","338,515","1,844,256"],["24/12/2020","344,311","1,873,102"],["25/12/2020","347,499","1,887,804"],["26/12/2020","349,152","1,896,298"],["27/12/2020","351,712","1,908,303"],["28/12/2020","354,848","1,923,669"],["29/12/2020","363,515","1,954,539"],["30/12/2020","371,194","1,987,732"],["31/12/2020","382,967","2,037,256"],["1/1/2021","387,062","2,053,280"],["2/1/2021","389,319","2,063,758"],["3/1/2021","391,354","2,071,114"],["4/1/2021","396,447","2,087,535"],["5/1/2021","405,866","2,119,794"],["6/1/2021","414,825","2,153,320"],["7/1/2021","423,586","2,188,409"],["8/1/2021","431,978","2,221,398"],["9/1/2021","438,920","2,249,492"],["10/1/2021","441,998","2,262,943"],["11/1/2021","445,210","2,272,778"],["12/1/2021","449,461","2,289,403"],["13/1/2021","454,567","2,310,220"],["14/1/2021","466,857","2,357,299"],["15/1/2021","478,666","2,398,159"],["16/1/2021","485577","2,429,571"],["17/1/2021","487,652","2,440,201"],["18/1/2021","491,243","2,456,218"],["19/1/2021","495,130","2,486,224"],["20/1/2021","501,678","2,517,951"],["21/1/2021","506,367","2,545,628"],["22/1/2021","512,457","2,578,198"],["23/1/2021","516,896","2,604,423"],["24/1/2021","518,529","2,617,046"],["25/1/2021","521,152","2,630,948"],["26/1/2021","524,884","2,653,596"],["27/1/2021","529,106","2,681,118"],["28/1/2021","","0"],["29/1/2021","","0"],["30/1/2021","","0"],["31/1/2021","","0"],["1/2/2021","","0"],["2/2/2021","","0"],["3/2/2021","","0"],["4/2/2021","","0"],["5/2/2021","","0"],["6/2/2021","","0"],["7/2/2021","","0"]]],"fileName":"titulos_antigenos","sheetNames":["total","positivas","ips","departamentos","Histórico"],"version":"57","refreshed":1611777255000}
I've tried using the split() method but it doesn't work. Can anyone help? Thanks
That's literally valid JSON. This works:
import json
my_string = '{"data":[[[""],["2,681,118"]], ...}'
nested_dict = json.loads(my_string)

extracting string within python list [duplicate]

This question already has answers here:
How to convert string representation of list to a list
(19 answers)
Closed 4 years ago.
How do I split this:
str = "['A20150710', 1.0]" to get 'A20150710'?
I tried the below line but not sure how to proceed from there:
str.split(',')
Use ast.literal_eval to convert string representation to a list and get the first item:
import ast
str = "['A20150710', 1.0]"
print(ast.literal_eval(str)[0])
# A20150710
Split on , and remove punctuations
import string
str1 = "['A20150710', 1.0]"
str1=str1.split(',')[0]
str1.translate(None,string.punctuation) #'A20150710'
Use eval to parse the string, then fetch the information you want
str = "['A20150710', 1.0]"
eval(str)[0] # A20150710
!!!Be careful!!! Using eval is a security risk, as it executes arbitrary Python expressions

how to convert string into dictionary in python 3.*? [duplicate]

This question already has answers here:
Convert a String representation of a Dictionary to a dictionary
(11 answers)
Closed 6 years ago.
I want to convert the following string into dictionary without using eval() function in Python 3.5.
d="{'Age': 7, 'Name': 'Manni'}";
Can anybody tell me the good way than using the eval() function?
What I really want is a function which can directly convert a dictionary to a string.
literal_eval, a somewhat safer version of eval (will only evaluate literals ie strings, lists etc):
from ast import literal_eval
python_dict = literal_eval("{'a': 1}")
json.loads but it would require your string to use double quotes:
import json
python_dict = json.loads('{"a": 1}')

Python convert user input properly formatted as an array into an int array [duplicate]

This question already has answers here:
How to turn a string into a list in python?
(2 answers)
Closed 8 years ago.
I was wondering if there was a way to take a properly formatted user input array, IE '[2,1,1]' as user input, and then convert that into a int array of [2,2,1].
You could use ast.literal_eval to safely parse a string representing a literal Python expression into a Python object:
import ast
ast.literal_eval('[2,1,1]')
# [2, 1, 1]
If the string is valid JSON, then you could instead use json. loads:
import json
json.loads('[2,1,1]')
# [2, 1, 1]
Of the two, when the string is both a literal Python expression and JSON, json.loads is faster.

Categories