Removing single quotes from a list of string [duplicate] - python

This question already has answers here:
I want to replace single quotes with double quotes in a list
(3 answers)
Closed 3 years ago.
I have a list of products which are strings & want to remove single quotes from it
The list of products retrieved from a sql query in python environment
['000003286694', '000003286704', '000003420412']
I have to pass this list as a payload to json object. So it needs to be preceded by double quotes.
I have used following method
d=[('"'+i+'"') for i in x]
but I get the output as
['"000003286694"', '"000003286704"', '"000003420412"']
I tried using regex, replace method to get away with single quotes but no success.
Can anyone please share workaround for this
I want result list should be
["000003286694", "000003286704", "000003420412"]

data =['000003286694', '000003286704', '000003420412']
import json
result =json.dumps(data)
print(result) #["000003286694", "000003286704", "000003420412"]

Related

remove the [''] when printing a list in python [duplicate]

This question already has answers here:
Print list without brackets in a single row
(14 answers)
Closed 5 months ago.
For some reason when I print a list like
list = []
list.append("0")
list.append("1")
print(list[0])
the output will be ["0"]
My actual code is a large block of text. Here's a link to the actual code: https://pastebin.com/Z54NfivR
Try this:
print(*list)
This essentially unpacks your list and its elements are treated as if they were separated by commas in the print function.
I used the name list because that was included in your example but it is a good practice to avoid using python commands as variable names.

How to force a dictionary to be written with " " instead of ' ' [duplicate]

This question already has answers here:
Convert single quotes to double quotes for Dictionary key/value pair
(2 answers)
Closed 10 months ago.
So I am working on a project in which I build an ongoing dictionary for kennings (its for a norse mythology course). The problem I run into is on occasion a kenning has an apostrophe in it. For example the kenning "wolf's joint" which has the definition "joint" gets written to the dictionary as {"wolf's wrist": 'joint'}, this would be fine if the json.loads() function didn't through up an error because the key has "" and the value has ''. I was wondering if there is a way to force a dict to be written always with the "" instead of ''.
You can try json.dumps:
>>> print(json.dumps({"wolf's wrist": 'joint'}))
{"wolf's wrist": "joint"}

python format list into comma separated string [duplicate]

This question already has answers here:
How to concatenate (join) items in a list to a single string
(11 answers)
Closed 3 years ago.
I have a simple list
li=['beststreet','borocd']
print "I like strings a lot {}".format(li)
I want to format the list into a string but I want the output to be like this:
I like strings a lot beststreet,borocd
background: this is a simple example, I am doing string manipulation in a Class to be inserted into a SQL table...
Just join the strings in the list with a comma:
li=['beststreet','borocd']
print "I like strings a lot {}".format(‘,’.join(li))

escaping {} brackets in triple quote Python strings [duplicate]

This question already has answers here:
How do I escape curly-brace ({}) characters in a string while using .format (or an f-string)?
(23 answers)
Closed 3 years ago.
how can you format a string of this form in Python 3?
'''{name}{{name}}'''.format(name="bob")
the desired output is: bob{bob}, but the above gives: bob{name}.
one solution is to add another argument to format:
'''{name1}{name2}'''.format(name1="bob", name2="{bob}")
but this is excessive. is there a way to properly escape { such that inner {x} can still be interpolated and one can only pass a single name to format?
Add one more level of {}:
'''{name}{{{name}}}'''.format(name="bob")
which outputs:
bob{bob}

Add element with single quote to dictionary in Python [duplicate]

This question already has answers here:
Using quotation marks inside quotation marks
(12 answers)
Closed 6 years ago.
I created dictionary in python like below:
flip= {'0': 'a"', '1':'b"', '2':'c"'}
But I don't want to use double quotes. I need elements with single quotes.
How can I do something like below? I was trying with \\, \, but it seems not to work.
Correct dict should look like below:
flip= {'0': 'a'', '1':'b'', '2':'c''}
You can simply use double quotes around the element.
lip= {'0': "a'", '1':"b'", '2':"c'"}
Using backslash should work, have you tried below?
flip= {'0': 'a\'', '1':'b\'', '2':'c\''}

Categories