Python remove double quotes from list

I am trying to get some data in a list of dictionaries. The data comes from a csv file so it's all string. the the keys in the file all have double qoutes, but since these are all strings, I want to remove them so they look like this in the dictionary:

{'key':value}

instead of this

{'"key"':value}

I tried simply using string = string[1:-1], but this doesn's work...

Here is my code:

csvDelimiter = ","
tsvDelimiter = "\t"
dataOutput = []

dataFile = open("browser-ww-monthly-201305-201405.csv","r")

for line in dataFile:
    line = line[:-1]    # Removes \n after every line

    data = line.split(csvDelimiter)

    for i in data:
        if type(i) == str:    # Doesn't work, I also tried if isinstance(i, str)
                              # but that didn't work either.
            print i
            i = i[1:-1]
            print i
    dataOutput.append({data[0] : data[1]})

dataFile.close()

print "Data output:\n"
print dataOutput

all the prints I get from print i are good, without double quotes, but when I append data to dataOutput, the quotes are back!

Any idea how to make them disappear forever?

Remove double quotes from a List of strings in Python #

To remove the double quotes from a list of strings:

  1. Use a list comprehension to iterate over the list.
  2. Use the str.replace() method to remove the double quotes from each string.
  3. The items in the new list won't contain double quotes.

Copied!

my_list = ['"one"', '"two"', '"three"'] # ✅ Remove all quotes from a list of strings new_list = [item.replace('"', '') for item in my_list] print(new_list) # 👉️ ['one', 'two', 'three'] # ------------------------------------------------- # ✅ Remove the leading and trailing quotes from a list of strings new_list = [item.strip('"') for item in my_list] print(new_list) # 👉️ ['one', 'two', 'three']

We used a list comprehension to remove the double quotes from a list of strings.

List comprehensions are used to perform some operation for every element or select a subset of elements that meet a condition.

On each iteration, we remove all occurrences of a double quote by replacing each with an empty string.

The list comprehension returns a new list, in which the strings don't contain double quotes.

The str.replace method returns a copy of the string with all occurrences of a substring replaced by the provided replacement.

The method takes the following parameters:

NameDescription
old The substring we want to replace in the string
new The replacement for each occurrence of old
count Only the first count occurrences are replaced (optional)

The method doesn't change the original string. Strings are immutable in Python.

Alternatively, you can use the str.strip() method.

To remove the double quotes from a list of strings:

  1. Use a list comprehension to iterate over the list.
  2. Use the str.strip() method to remove the leading and trailing double quotes from each string.
  3. The items in the new list won't contain leading and trailing double quotes.

Copied!

my_list = ['"one"', '"two"', '"three"'] new_list = [item.strip('"') for item in my_list] print(new_list) # 👉️ ['one', 'two', 'three']

The example uses the str.strip() method to remove the leading and trailing double quotes from each string in the list.

The str.strip method takes a string containing characters as a parameter and removes all occurrences of the characters from the front and back of the string.

We passed a double quote to the strip() method to remove all occurrences of a double quote at the front and end of each string.

The strip() method doesn't remove all occurrences of the specified character from the string, it only removes the occurrences at the front and end.

Copied!

# 👇️ 'hello " world' print('"hello " world"'.strip('"'))

You can also use the str.lstrip() and str.rstrip() methods, which remove the leading or the trailing occurrences of the specified character.

How do you remove double quotes from a list in Python?

Use the str. strip() method to remove the leading and trailing double quotes from each string. The items in the new list won't contain leading and trailing double quotes.

How do you remove quotes from a list in Python?

replace() to remove all quotes from a string. Call str. replace(old, new) on a string with the quote character '"' as old and an empty string "" as new to remove all quotes from the string.

How do you remove quotes from a list of strings?

To remove the quotes from a list of strings: Use a list comprehension to iterate over the list. Use the str. replace() method to remove the quotes from each string.

How do you replace double quotes in Python?

So here in our first statement, we first generate a string with double quotes. Then we call the rstrip() function and pass ('\')as a parameter to remove double-quotes. Then we use two print functions. The first one displays the original string and the second one displays the new filtered string.