Python Forum
Remove double quotes from the list ?
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Remove double quotes from the list ?
#1
I have the following code below:

 from cgi import FieldStorage
 v_field = FieldStorage()
 v_field.list = [v_str_sev]
 print(v_field)
 >> FieldStorage(None, None, ["MiniFieldStorage('v_name', 'João'), MiniFieldStorage('v_sname', 'Aguiar')"])
How do I remove the double quotes keeping the data type as class (FieldStorage) and not (string)? Using regex the data is converted to a string but I need the data as <class 'cgi.FieldStorage'> ?
Reply
#2
Strings in python that have single quotes inside are printed with double quotes.

>>> s = 'foo\'bar'
>>> s
"foo'bar"
>>> [s]
["foo'bar"]
If you printed out v_str_sev, you should see the same thing. As your string has single quotes inside, it's printed with surrounding double-quotes. The question I have is why do you care that there are double-quotes around your string? What problem is it causing?
Reply
#3
FieldStorage(None, None, ["MiniFieldStorage('v_name', 'João'), MiniFieldStorage('v_sname', 'Aguiar')"])

The list in your FieldStorage list contains only one string, surrounded by double quotes:
Output:
MiniFieldStorage('v_name', 'João'), MiniFieldStorage('v_sname', 'Aguiar')
Chamge to:
FieldStorage(None, None, ["MiniFieldStorage('v_name', 'João')", "MiniFieldStorage('v_sname', 'Aguiar')"
and the list now contains two strings:
Output:
MiniFieldStorage('v_name', 'João') MiniFieldStorage('v_sname', 'Aguiar')
Reply
#4
(Oct-31-2020, 02:55 AM)bowlofred Wrote: Strings in python that have single quotes inside are printed with double quotes.

>>> s = 'foo\'bar'
>>> s
"foo'bar"
>>> [s]
["foo'bar"]
If you printed out v_str_sev, you should see the same thing. As your string has single quotes inside, it's printed with surrounding double-quotes. The question I have is why do you care that there are double-quotes around your string? What problem is it causing?

Sure.

Your print to (v_str_sev), the result will be:
 MiniFieldStorage('v_name', 'João'), MiniFieldStorage('v_sname', 'Aguiar'), without square brackets and double quotes.


The correct result of the FieldStorage class is defined:
 FieldStorage(None, None, [MiniFieldStorage('v_name', 'João'), MiniFieldStorage('v_sname', 'Aguiar')]) 
Note that there are no double quotes so I need to remove the double quotes if I cannot capture the form fields using,
 getvalue()
Reply
#5
Before line4, if you add this, what is the output? I can't tell where your v_str_sev is getting its data.

print(type(v_str_sev))
If it's a str, then you need to figure out where the data is coming from and fix it there.
Reply
#6
(Oct-31-2020, 06:20 PM)bowlofred Wrote: Before line4, if you add this, what is the output? I can't tell where your v_str_sev is getting its data.

print(type(v_str_sev))
If it's a str, then you need to figure out where the data is coming from and fix it there.

The output is :
 <class 'str'>
And when I press the variable (v_str_sev), the result I have is exactly this:
   print(v_str_sev)
   -> MiniFieldStorage('v_name', 'João'), MiniFieldStorage('v_sname', 'Aguiar')
Note that there are no double quotes at the beginning and end.

Double quotes appear only when I re-string the string by inserting the variable (v_str_sev) between the brackets as the value of the attribute (list) of the instantiated v_field object of the FieldStorage class, as shown in the example below :
   from cgi import FieldStorage
   v_field = FieldStorage()
   v_field.list = [v_str_sev]
   print(v_field)

   -> FieldStorage(None, None, ["MiniFieldStorage('v_name', 'João'), MiniFieldStorage('v_sname', 'Aguiar')"])
Note that the data type is now, that it is almost correct, missing the double quotes from the beginning and end ?
   <class 'cgi.FieldStorage'>
   -> FieldStorage(None, None, ["MiniFieldStorage('v_name', 'João'), MiniFieldStorage('v_sname', 'Aguiar')"])
Reply
#7
I am confused. You cannot do this: "How do I remove the double quotes keeping the data type as class (FieldStorage) and not (string)?". Everything that is printed is converted to a string, and the result is None with a side effect of drawing the characters to stdout. I occasionally see posts here where the author uses a print statement to display data and is concerned about an decorations that might be appended or removed by the print statement. The print statement is only displaying the result of __str__(thing) or __repr__(thing), and not the actual thing. Why do you think it is important to not have the quotes? Sure you can change how the string is constructed and avoid the quotes, but it is still a string.

To expand slightly on bolofred's example:
a = 'foo\'bar'
b = 'foobar'
print(a, b)
print([a, b])
print(f'[{a}, {b}]')
Output:
foo'bar foobar ["foo'bar", 'foobar'] [foo'bar, foobar]
When printing strings that are in a list, Python wraps the string in quotes. If the string includes a single quote Python wraps the string in double quotes. To remove the quotes you have to format the string yourself. The last print does not show quotes because it is not printing a list. It prints two strings surrounded by brackets. I don't know if this is applicable to your problem.
Reply
#8
(Nov-01-2020, 01:40 AM)PythonDev Wrote: The output is :
 <class 'str'>

This is your problem. Rather than storing the actual MiniFieldStorage object, it is storing just a string. Find out where this variable is assigned and capture the object instead.
Reply
#9
(Nov-01-2020, 04:28 AM)bowlofred Wrote:
(Nov-01-2020, 01:40 AM)PythonDev Wrote: The output is :
 <class 'str'>

This is your problem. Rather than storing the actual MiniFieldStorage object, it is storing just a string. Find out where this variable is assigned and capture the object instead.

The problem is that the results of the attribute (list) of the instantiated object add the double quotes without existing in the variable string (v_str_sev), why would it be ?
ndc85430 likes this post
Reply
#10
The problem is that you will not accept what the problem really is. You have a string inside a list. If you print that list it will wrap the string inside quotes. If there is a single quote in the string Python wraps the string in double quotes. Bowlofred said this way back at the beginning and you refuse to accept it. I don't know why the list.__str__ code was written to do this, but that is what happens. And as Bowlofred has also said, if you want to get rid of the surrounding quotes you need the thing in the list to not be a string.

Looks like you have 3 choices.
1. Stop caring about the quotes (my favorite)
2. Change your code to: v_field.list = [something_that_is_not_a_string]
3. Write you own code to format the output

Edit:
I think I just realized why you care about the quotes. You are printing v_field while trying to debug a problem. You think the problem is that there are double quotes around v_str_sev, but that is just a side effect of how print works. The real problem is that v_str_sev is not what you should be putting in v_field.list. Since you've never said why you care about the double quotes this is only a guess.
buran and ndc85430 like this post
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Remove single and double quotes from a csv file in 3 to 4 column shantanu97 0 1,164 Mar-31-2021, 10:52 AM
Last Post: shantanu97
  .remove() from a list - request for explanation InputOutput007 3 558 Jan-28-2021, 04:21 PM
Last Post: InputOutput007
  Remove specific elements from list with a pattern Xalagy 3 778 Oct-11-2020, 07:18 AM
Last Post: Xalagy
  How to remove dict from a list? Denial 7 800 Sep-28-2020, 02:40 PM
Last Post: perfringo
  Quotes vs. no quotes around numbers Mark17 6 836 Aug-06-2020, 04:13 AM
Last Post: t4keheart
  Help: list comprehension for loop with double if statement mart79 3 791 May-04-2020, 06:34 AM
Last Post: buran
  Remove all \n from end of list items JackMack118 4 28,963 Dec-27-2019, 08:34 AM
Last Post: perfringo
  can't remove element from a list yokaso 6 1,042 Nov-06-2019, 03:11 PM
Last Post: yokaso
  Cannot Remove the Double Quotes on a Certain Word (String) Python BeautifulSoup soothsayerpg 5 3,493 Oct-27-2019, 09:53 AM
Last Post: newbieAuggie2019
  with input remove a string from the list konsular 3 960 Oct-12-2019, 09:25 AM
Last Post: konsular

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020