We use some essential cookies to make our website work.

We use optional cookies, as detailed in our cookie policy, to remember your settings and understand how you use our website.

12 posts • Page 1 of 1
Duggieb
Posts: 31
Joined: Wed May 26, 2021 8:39 pm

Parsing string

Mon Oct 13, 2025 7:07 pm

I have a data string from an HTML form using the GET method as below, the string obtained by the Python script os.environ.get('QUERY_STRING')

name=sam&comment=sam+comment&answer=red&question_no=3&submit=Submit

Can anyone give me some guidance as to how to parse this string in Python so I can have a variable 'name 'with value 'sam', variable 'comment ' with value 'sam comment' etc? My first thoughts are to read each character in turn until I find a separating character (eg = or &) and then extract the data that follows. is there an easier way?

gordon77
Posts: 9265
Joined: Sun Aug 05, 2012 3:12 pm

Re: Parsing string

Mon Oct 13, 2025 7:15 pm

You could start with split

x = name.split("=")

Duggieb
Posts: 31
Joined: Wed May 26, 2021 8:39 pm

Re: Parsing string

Mon Oct 13, 2025 7:56 pm

This produces ['name', 'pete&comment', 'pete+comment&answer', 'red&question_no', '3&method', 'get&submit', 'Submit']. Is the next step y=x.split
Last edited by Duggieb on Mon Oct 13, 2025 8:01 pm, edited 1 time in total.

ame
Posts: 11565
Joined: Sat Aug 18, 2012 1:21 am

Re: Parsing string

Mon Oct 13, 2025 7:58 pm

You could try

x = name.split("&")
Oh no, not again.

ame
Posts: 11565
Joined: Sat Aug 18, 2012 1:21 am

Re: Parsing string

Mon Oct 13, 2025 8:00 pm

Duggieb wrote:
Mon Oct 13, 2025 7:56 pm
This produces ['name', 'pete&comment', 'pete+comment&answer', '7&question_no', '5&method', 'get&submit', 'Submit']
That's amazing, because you started with "sam".
Oh no, not again.

Duggieb
Posts: 31
Joined: Wed May 26, 2021 8:39 pm

Re: Parsing string

Mon Oct 13, 2025 8:02 pm

Sorry, I was about to edit that. Is the next step y=x.split("&")?

DougieLawson
Posts: 43604
Joined: Sun Jun 16, 2013 11:19 pm

Re: Parsing string

Mon Oct 13, 2025 8:12 pm

I would add an ampersand at the start of the string. Split on & so every part of the string becomes &variable=value or &variable=value1+value2.
Then evaluate each section (&variable=value) to assign the values to the variables. The logical option is a store everything in a python dictionary structure.

There used to be a python cgi.py module that did all that stuff for you but TPTB removed it from support with Python 3.13.

Your string would become

Code: Select all

dict = {"name":"sam", 
 "comment":"sam+comment",
 "answer":"red",
 "question_no":3,
 "submit":"Submit"
}
Languages using left-hand whitespace for syntax are ridiculous

DMs sent on Bluesky or by LinkedIn will be answered next month.
Fake doctors - are all on my foes list.

The use of crystal balls and mind reading is prohibited.

rpiMike
Posts: 3636
Joined: Fri Aug 10, 2012 12:38 pm

Re: Parsing string

Mon Oct 13, 2025 8:28 pm

Another option:

Code: Select all

import urllib.parse
qs = "name=sam&comment=sam+comment&answer=red&question_no=3&submit=Submit"
x = urllib.parse.parse_qs(qs)
print("x",x)
for y in x:
 print("key:",y,"value:",x[y][0])
 
print(x["name"][0])

Code: Select all

x {'name': ['sam'], 'comment': ['sam comment'], 'answer': ['red'], 'question_no': ['3'], 'submit': ['Submit']}
key: name value: sam
key: comment value: sam comment
key: answer value: red
key: question_no value: 3
key: submit value: Submit
sam

scruss
Posts: 6569
Joined: Sat Jun 09, 2012 12:25 pm

Re: Parsing string

Mon Oct 13, 2025 11:36 pm

rpiMike wrote:
Mon Oct 13, 2025 8:28 pm
Another option:

Code: Select all

import urllib.parse
...
That's the one. urllib is a standard library, so it should be available. It has millions of users, so the critical flaws should be hammered out by now. Don't roll your own code that parses data from random users on the internet: bored and/or malicious actors can do a lot of harm.
‘Remember the Golden Rule of Selling: "Do not resort to violence."’ — McGlashan.
Pronouns: he/him

gordon77
Posts: 9265
Joined: Sun Aug 05, 2012 3:12 pm

Re: Parsing string

Tue Oct 14, 2025 8:53 am

scruss wrote:
Mon Oct 13, 2025 11:36 pm
Don't roll your own code that parses data from random users on the internet: bored and/or malicious actors can do a lot of harm.
:o :lol:

DeaD_EyE
Posts: 247
Joined: Sat May 17, 2014 9:49 pm

Re: Parsing string

Tue Oct 14, 2025 3:37 pm

Task: Program a secure web server, which fulfills all standards
Me: NO

Duggieb
Posts: 31
Joined: Wed May 26, 2021 8:39 pm

Re: Parsing string

Wed Oct 15, 2025 5:54 pm

Thank you very much, all above, for taking the time and effort to reply to my question. Learning that os.environ.get accesses a GET method query string from an HTML form and then urllib.parse.parse(query string) gives a dictionary meant my website is now operational again after CGI was deprecated in Python 3.13 I am really enjoying learning Python and becoming less than a complete beginner!!
My website is http://robinboardman.me.uk - as the header says, 'Do leave a message in the Guestbook'

12 posts • Page 1 of 1

Return to "Python"

AltStyle によって変換されたページ (->オリジナル) /