i have following dictionary and I need to remove the unnecessary double quote around the keys and values in the dictionary object:
d={" 'John'": "'car': 2, 'laptop': 4, 'comp': 3"," 'Jim'": "'car':2, 'laptop':3 ,'computer':2"}
I want the dictionary object to be like:
d={'John': 'car': 2, 'laptop': 4, 'comp': 3,'Jim':'car':2, 'laptop':3 ,'computer':2}
here's the code that I tried but gives error:
ast.literal_eval[d.replace['""', '"']]
initially i had a string object which i tried to parse to dict and I got the above initial d={} so when I try to do
print[d['john']] gives error
but when I do d["'john'"] it prints the correct value. so I was trying to fixt it
Based on the example you provided, you probably shouldn't. It looks like that value is actually a json object. The double quotes are a part of the spec. So either somebody pickled something wrong, or that value is a JSON string for a very specific reason.
If you're trying to read it out or convert it to a python dictionary, you could take the value and pass it to json.loads
and you'll get a dictionary back. Thing is, you won't ve able to 'know' something is or
isn't a JSON string unless you have a spec. You could do a try/except and test the value... Set the dict if the loads method works, leave it as is if it doesn't...
Introduction
There are several ways you can remove quotes from a string in Python. You may need to remove all quotes or just ones surrounding a string. You may also need to remove single or double quotes.
In this short article, we've compiled a comprehensive list of methods you can use to remove quotes from a string in Python. It's absolutely up to you which one you'll use. Please note that the solutions we've listed here are not ordered from best to worst. Each solution is good as long as it meets your needs in a specific case.
How to Remove All Quotes from a String in Python
First of all, let's take a look at how to remove all quotes from a string. Solutions listed in this section can be altered to work with both single and double quotation marks. But, for the purpose of this article, let's say we have an example string that has three double quotation marks in it:
example_str = '"This is a string" with quotes"'
In this section, we'll take a look at several ways how to remove all "
from the example_str
.
str.replace[]
The first approach we'll discuss is to use the str.replace[]
method on the example_str
. It accepts two arguments - the string we want to replace and the replacement string. In this case, we'll replace a double quotation mark ["
] with an empty string:
new_str = example_str.replace['"', '']
print[new_str] # This is a string without quotes
The str.replace[]
used in this way will remove all double quotes from the
example_str
.
Regular Expressions
This approach uses regular expressions to remove all quotes from a string. Before we start using regular expressions in Python, we first need to import the re
module:
import re
After that, we can use the re.sub[]
method to substitute all occurrences of the quotation mark with the empty string:
new_str = re.sub['"', '', example_str]
print[new_str] # This is a string without quotes
This will give us the example_str
without any quotes in it.
str.join[]
The str.join[]
is another method we can use to remove all quotes from a string in Python. This solution has a bit more complex syntax than other solutions if you are not already familiar with the join[]
method. We'll essentially go over the example_str
character-by-character and append each which is not a quotation mark to the empty string. That way, we'll get the example_str
without any quotes in it:
new_str = ''.join[c for c in example_str if c not in '"']
print[new_str] # This is a string without quotes
How to Remove Quotes Surrounding a String
So far, we've been discussing several approaches on how to remove all quotation marks from a string in Python. That's only one way of looking at removing quotes from a string. Arguably, the more common problem is how to remove only quotes surrounding a string. The str.strip[]
method is intended to do just that. Therefore, we'll discuss the str.strip[]
method in this section.
Say we have the same exact example_str
as in the previous section, and we want to remove only
the first and the last quotation marks:
example_str = '"This is a string" with quotes"'
Using the str.strip[]
with a double qoutation mark as its argument will remove leading and trailing quotation marks from the example_str
:
Check out our hands-on, practical guide to learning Git, with best-practices, industry-accepted standards, and included cheat sheet. Stop Googling Git commands and actually learn it!
new_str = example_str.strip['"']
print[new_str] # This is a string" without quotes
Note: No matter how many leading and/or leading
quotation marks are in the string, the str.strip[]
will remove all of them.
Besides the usual str.strip[]
method, there are also two of its derivatives - str.lstrip[]
and str.rstrip[]
. The first one removes all leading quotes, and the second removes all trailing quotes:
new_str = example_str.lstrip['"']
print[new_str] # This is a string" without quotes"
new_str = example_str.rstrip['"']
print[new_str] # "This is a string" without quotes
Conclusion
In this short article, we've taken a comprehensive look at how to remove quotes from a string in Python. First of all, we've explained how to remove all quotes from
a string using several different methods - str.replace[]
, str.join[]
, and re.sub[]
. Afterward, we've taken a look at the str.strip[]
method, as well as its derivatives str.lstrip[]
and str.rstrip[]
.