Parsing string
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?
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?
Re: Parsing string
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.
Re: Parsing string
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
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
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.
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.
Re: Parsing string
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
samRe: Parsing string
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
Pronouns: he/him
Re: Parsing string
Task: Program a secure web server, which fulfills all standards
Me: NO
Me: NO
Re: Parsing string
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'
My website is http://robinboardman.me.uk - as the header says, 'Do leave a message in the Guestbook'
Jump to
- Community
- General discussion
- Announcements
- Other languages
- Deutsch
- Español
- Français
- Italiano
- Nederlands
- 日本語
- Polski
- Português
- Русский
- Türkçe
- User groups and events
- Raspberry Pi Official Magazine
- Using the Raspberry Pi
- Beginners
- Troubleshooting
- Advanced users
- Assistive technology and accessibility
- Education
- Picademy
- Teaching and learning resources
- Staffroom, classroom and projects
- Astro Pi
- Mathematica
- High Altitude Balloon
- Weather station
- Programming
- C/C++
- Java
- Python
- Scratch
- Other programming languages
- Windows 10 for IoT
- Wolfram Language
- Bare metal, Assembly language
- Graphics programming
- OpenGLES
- OpenVG
- OpenMAX
- General programming discussion
- Projects
- Networking and servers
- Automation, sensing and robotics
- Graphics, sound and multimedia
- Other projects
- Media centres
- Gaming
- AIY Projects
- Hardware and peripherals
- Camera board
- Compute Module
- Official Display
- HATs and other add-ons
- Device Tree
- Interfacing (DSI, CSI, I2C, etc.)
- Keyboard computers (400, 500, 500+)
- Raspberry Pi Pico
- General
- SDK
- MicroPython
- Other RP2040 boards
- Zephyr
- Rust
- AI Accelerator
- AI Camera - IMX500
- Hailo
- Software
- Raspberry Pi OS
- Raspberry Pi Connect
- Raspberry Pi Desktop for PC and Mac
- Beta testing
- Other
- Android
- Debian
- FreeBSD
- Gentoo
- Linux Kernel
- NetBSD
- openSUSE
- Plan 9
- Puppy
- Arch
- Pidora / Fedora
- RISCOS
- Ubuntu
- Ye Olde Pi Shoppe
- For sale
- Wanted
- Off topic
- Off topic discussion