Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Python str() double quoted string

I know this question sounds very naive but I am stuck with it. I am using jquery autocomplete and it turns out that it needs ["data1", "data2"] form of list.

I am getting data from a json file and

data = json.load(fobj)['content']
data = [str(data[d]['name']) for d in data.keys()]

['some text', 'some other '....]

but I specifically want a double quoted string in list. I have tried repr(str(data)) but in it gives "'some text'" and then "'some text'".strip("'") but again this returns a single quoted string.

To get the work done i am doing

data = [str(data[d]['name'] + "'") for d in data.keys()]

and later process the "'" at the end of string but this is not the right way.

Is there any way with which i can force str() to return double quoted string something like str(data, quote='"').

like image 861
Amit Tripathi Avatar asked Feb 21 '26 18:02

Amit Tripathi


1 Answers

jQuery means JavaScript. JavaScript literals means JSON. Just re-encode.

>>> json.dumps(['foo', '''bar'''])
'["foo", "bar"]'
like image 86
Ignacio Vazquez-Abrams Avatar answered Feb 23 '26 07:02

Ignacio Vazquez-Abrams



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!