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 ?
#11
(Nov-01-2020, 03:00 PM)deanhystad Wrote: 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.

I will try to explain what is happening for a better understanding:

1. I capture the form fields using the cgi module using the FieldStorage class, saving the result in a variable:
array_input = FieldStorage (None, None, [MiniFieldStorage ('v_name', 'John'), MiniFieldStorage ('v_sname', 'Aguiar')])
2. I need to pass the data obtained through a threat detection process by removing the parentheses from the value of the variable (array_input). And for this to happen I change the <class 'cgi.FieldStorage'> data type to <class 'str'> and be able to manipulate the string using regular expressions.

3. After removal and the security analysis process, I need to reassemble the data that is in the <class 'str'> format using regular expressions and change the data type to the original start format <class 'cgi.FieldStorage'> and be able to manipulate the form fields.

4. See the reassembly process:

# I get the data unmounted:
array_input = "FieldStorage None, None, [MiniFieldStorage 'v_name', 'John' , MiniFieldStorage 'v_sname', 'Aguiar' ]"

# 1. Redo the string assembly:
v_build_one = "FieldStorage(None, None, [MiniFieldStorage("
v_build_two = "), MiniFieldStorage('"
v_build_tre = ")])"
v_build_for = "[MiniFieldStorage("

import re

v_str_one = re.sub(r"FieldStorage None, None, \[MiniFieldStorage", v_build_one, array_input)
v_str_two = re.sub(r" , MiniFieldStorage '", v_build_two, v_str_one)
v_str_tre = re.sub(r" ]", v_build_tre, v_str_two)
v_str_for = re.sub(r"\[MiniFieldStorage\( ", v_build_for, v_str_tre)
v_str_fiv = re.sub(r"FieldStorage\(None, None, ", "", v_str_for)
v_str_six = re.sub(r"\]\)", "", v_str_fiv)
v_str_sev = re.sub(r"\[", "", v_str_six)

from cgi import FieldStorage
v_field = FieldStorage()
v_field.list = [v_str_sev]
print(v_field)

output with double quote error:  FieldStorage(None, None, ["MiniFieldStorage('v_name', 'John'), MiniFieldStorage('v_sname', 'Aguiar')"])
Reply
#12
I have zero experience with CGI, but the way you rebuild script looks very specific. I wrote something equally specific but use f'string.

It appears that all you are trying to do is get the key and value for the MiniFieldStorage, and it looks like there will always be two MiniFieldStorage objects in the list. Instead of trying to replace the parts that are formatted wrong I approached this by getting the parts I am interested in and building the string from fresh.
array_input = "FieldStorage None, None, [MiniFieldStorage 'v_name', 'John' , MiniFieldStorage 'v_sname', 'Aguiar' ]"
 
fields = array_input.replace('FieldStorage None, None, [', '')  # throw away stuff we don't care about
fields = fields.replace('MiniFieldStorage', '').replace(']', '').replace(' ', '')
fields = fields.split(',')  # Get the things we want

# Rebuild the string
vbuild = f'FieldStorage(None, None, [MiniFieldStorage({fields[0]}, {fields[1]}), MiniFieldStorage({fields[2]},{fields[3]})]'
print(vbuild)
Im sure this could be made much better using regular expressions to find MiniFieldStorage and then getting the next two strings.

The end result is still a string, but hopefully a string in the right format for parsing.
Reply
#13
the problem you have is somewhere before the code you show here

i.e.
how it happened that cgi.FieldStorage instance
FieldStorage (None, None, [MiniFieldStorage ('v_name', 'John'), MiniFieldStorage ('v_sname', 'Aguiar')])
become a str

"FieldStorage (None, None, [MiniFieldStorage ('v_name', 'John'), MiniFieldStorage ('v_sname', 'Aguiar')])"
somewhere you converted cgi.FieldStorage object int str object and that is where the problem is.

there are appropriate methods to parse cgi.FieldStorage and acceess the MiniFiledStorage objects in the list and their values. Don't manipulate strings


import cgi
# let recreate what you SHOULD have instead of str
array_input = cgi.FieldStorage()
array_input.list = [cgi.MiniFieldStorage('v_name', 'John'), cgi.MiniFieldStorage('v_sname', 'Aguiar')]

# what we have and how we access data
print(type(array_input))
print(type(array_input['v_name']))
print(array_input.getvalue('v_name'))
Output:
<class 'cgi.FieldStorage'> <class 'cgi.MiniFieldStorage'> John
ndc85430 likes this post
If you can't explain it to a six year old, you don't understand it yourself, Albert Einstein
How to Ask Questions The Smart Way: link and another link
Create MCV example
Debug small programs

Reply
#14
(Nov-01-2020, 09:18 PM)buran Wrote: the problem you have is somewhere before the code you show here

i.e.
how it happened that cgi.FieldStorage instance
FieldStorage (None, None, [MiniFieldStorage ('v_name', 'John'), MiniFieldStorage ('v_sname', 'Aguiar')])
become a str

"FieldStorage (None, None, [MiniFieldStorage ('v_name', 'John'), MiniFieldStorage ('v_sname', 'Aguiar')])"
somewhere you converted cgi.FieldStorage object int str object and that is where the problem is.

there are appropriate methods to parse cgi.FieldStorage and acceess the MiniFiledStorage objects in the list and their values. Don't manipulate strings


import cgi
# let recreate what you SHOULD have instead of str
array_input = cgi.FieldStorage()
array_input.list = [cgi.MiniFieldStorage('v_name', 'John'), cgi.MiniFieldStorage('v_sname', 'Aguiar')]

# what we have and how we access data
print(type(array_input))
print(type(array_input['v_name']))
print(array_input.getvalue('v_name'))
Output:
<class 'cgi.FieldStorage'> <class 'cgi.MiniFieldStorage'> John

It all starts with the code below:
# Retrieves form field entries in the environment variable (wsgi.input) and assigns the variable (array_input):
array_input = cgi.FieldStorage(environ["wsgi.input"], environ=environ)
After that I send the variable array_input via parameter to the method of another class that processes deconstruction of the value of the variable (array_input) so that I can remove the characters that conflict in the threat detection (xss), because I don't know if it is possible to manipulate the type <class 'cgi.FieldStorage'> using regular expressions from the python re module, below is the code that receives the parameter and decouples the variable:

# Function -> Importa module cgi que trata form request e re (regex)
import re

# Class Function ->
class ProcessInputPost:

     # Method Function ->
     def spark (v_response, v_uri, array_input):

         # Function -> Convert to tuple
         a_inp = (array_input)
         # Function -> Convert tuple to string:
         o_inp = str(a_inp)
         # Function -> Remove parentheses for checking data:
         v_inp = re.sub ("[()]", "", o_inp)
         # Function ->
         from cern.safe.threatdetect import ThreatDetect
         # Function ->
         return ThreatDetect.m_post (v_response, v_uri, array_input)


User has been warned for this post. Reason: Edit post content after receiving reply
Reply
#15
As people are trying to tell you: stop converting the object to a string. Use the appropriate methods and fields to access the data in the object for your needs (and see the docs for what those are).

Perhaps you need to learn about objects and classes?

Also, there's really no need to quote the entirety of other posts that are quite long.
Reply
#16
some thoughts/observations:

  1. Assuming that array_input is still cgi.FieldStorage object at the time when you pass it to this function - you don't change it inside the function. You work with different names during conversion and cleaning process. So at the end of the function array_input is exactly the same as it was in the beginning and that is what you pass to ThreatDetect.m_post(). You never use o_inp and v_inp.
  2. What package is cern.safe.threatdetect coming from? Can you provide link to pypi, homepage, docs??
  3. In any case I think you need to check the values of cgi.MiniFieldStorage objects - i.e. that is what is the [possibly unsafe] user input in the form fields, not the string representation of cgi.FieldStorage
  4. Look at this line a_inp = (array_input). Note that a_inp is NOT tuple. In order to be tuple it should be a_inp = (array_input,) - note the comma.
    >>> spam = 1
    >>> eggs = (spam)
    >>> type(eggs)
    <class 'int'>
    >>> eggs
    1
    >>> eggs = (spam,)
    >>> type(eggs)
    <class 'tuple'>
    >>> eggs
    (1,)
If you can't explain it to a six year old, you don't understand it yourself, Albert Einstein
How to Ask Questions The Smart Way: link and another link
Create MCV example
Debug small programs

Reply
#17
(Nov-02-2020, 08:36 AM)buran Wrote: some thoughts/observations:

  1. Assuming that array_input is still cgi.FieldStorage object at the time when you pass it to this function - you don't change it inside the function. You work with different names during conversion and cleaning process. So at the end of the function array_input is exactly the same as it was in the beginning and that is what you pass to ThreatDetect.m_post(). You never use o_inp and v_inp.
  2. What package is cern.safe.threatdetect coming from? Can you provide link to pypi, homepage, docs??
  3. In any case I think you need to check the values of cgi.MiniFieldStorage objects - i.e. that is what is the [possibly unsafe] user input in the form fields, not the string representation of cgi.FieldStorage
  4. Look at this line a_inp = (array_input). Note that a_inp is NOT tuple. In order to be tuple it should be a_inp = (array_input,) - note the comma.
    >>> spam = 1
    >>> eggs = (spam)
    >>> type(eggs)
    <class 'int'>
    >>> eggs
    1
    >>> eggs = (spam,)
    >>> type(eggs)
    <class 'tuple'>
    >>> eggs
    (1,)

now I rectified the code to clarify the understanding :

# Function -> Importa module cgi que trata form request e re (regex)
import re
 
# Class Function ->
class ProcessInputPost:
 
     # Method Function ->
     def spark (v_response, v_uri, array_input):
 
         # Function -> Convert to tuple
         a_inp = (array_input,)
         # Function -> Convert tuple to string:
         o_inp = str(a_inp)
         # Function -> Remove parentheses for checking data:
         array_input = re.sub ("[()]", "", o_inp)
         # Function ->
         from cern.safe.threatdetect import ThreatDetect
         # Function ->
         return ThreatDetect.m_post (v_response, v_uri, array_input)
On the return of the method on the last line the parameter (array_input) is no longer a <class 'cgi.FieldStorage'> becomes <class 'str'> because the python regular expression of the (re) module can only understand the type of string data.
bowlofred likes this post
Reply
#18
Wall Wall Wall Wall Wall Wall Wall Wall Wall Wall Wall Wall
If you can't explain it to a six year old, you don't understand it yourself, Albert Einstein
How to Ask Questions The Smart Way: link and another link
Create MCV example
Debug small programs

Reply
#19
Please, don't change post content after you get reply. This makes my answer look incorrect. I revert your edit.
If you can't explain it to a six year old, you don't understand it yourself, Albert Einstein
How to Ask Questions The Smart Way: link and another link
Create MCV example
Debug small programs

Reply
#20
(Nov-02-2020, 05:49 PM)buran Wrote: Please, don't change post content after you get reply. This makes my answer look incorrect. I revert your edit.

I did to correct my mistake and have a better understanding
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  unable to remove all elements from list based on a condition sg_python 3 434 Jan-27-2024, 04:03 PM
Last Post: deanhystad
  Remove numbers from a list menator01 4 1,323 Nov-13-2022, 01:27 AM
Last Post: menator01
  Reading list items without brackets and quotes jesse68 6 4,622 Jan-14-2022, 07:07 PM
Last Post: jesse68
  Remove empty keys in a python list python_student 7 3,019 Jan-12-2022, 10:23 PM
Last Post: python_student
  Remove an item from a list contained in another item in python CompleteNewb 19 5,717 Nov-11-2021, 06:43 AM
Last Post: Gribouillis
  Remove single and double quotes from a csv file in 3 to 4 column shantanu97 0 6,978 Mar-31-2021, 10:52 AM
Last Post: shantanu97
  Two types of single quotes Led_Zeppelin 2 1,907 Mar-15-2021, 07:55 PM
Last Post: BashBedlam
  .remove() from a list - request for explanation InputOutput007 3 2,220 Jan-28-2021, 04:21 PM
Last Post: InputOutput007
  Remove specific elements from list with a pattern Xalagy 3 2,699 Oct-11-2020, 07:18 AM
Last Post: Xalagy
  How to remove dict from a list? Denial 7 2,933 Sep-28-2020, 02:40 PM
Last Post: perfringo

Forum Jump:

User Panel Messages

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