Categories
Python Answers

How to get POST and GET variables in Python?

Sometimes, we want to get POST and GET variables in Python.

In this article, we’ll look at how to get POST and GET variables in Python.

How to get POST and GET variables in Python?

To get POST and GET variables in Python, we can use the cgi library.

For instance, we write

import cgi

form = cgi.FieldStorage()
print(form["username"])

to get the form field values with

cgi.FieldStorage()

in a dict.

Then we get the value of the form data with field name username with

form["username"]

It should be set when we submit a value from an input like

<input type="text" name="username">

Conclusion

To get POST and GET variables in Python, we can use the cgi library.

Categories
Python Answers

How to convert JSON string to dict using Python?

Sometimes, we want to convert JSON string to dict using Python.

In this article, we’ll look at how to convert JSON string to dict using Python.

How to convert JSON string to dict using Python?

To convert JSON string to dict using Python, we can use the json.loads method.

For instance, we write

import json

# ...

d = json.loads(j)
print(d['glossary']['title'])

to load the j JSON string into a dict with the json.loads method and assign the returned dict to d.

Then we can get the value we’re looking for from the dict d.

Conclusion

To convert JSON string to dict using Python, we can use the json.loads method.

Categories
Python Answers

How to call Python max function using ‘key’ and lambda expression?

Sometimes, we want to call Python max function using ‘key’ and lambda expression.

In this article, we’ll look at how to call Python max function using ‘key’ and lambda expression.

How to call Python max function using ‘key’ and lambda expression?

To call Python max function using ‘key’ and lambda expression, we can call max with the list and the key argument set to a function that returns the value that we want to sort by.

For instance, we write

def func(p):
   return p.total_score

m = max(players, key=func)

to call max with the players list and the key set to the func function.

In func, we return the total_score function from the p object that’s in the players list to sort by the total_score value from each object in players and get the item with the max value of total_score.

Conclusion

To call Python max function using ‘key’ and lambda expression, we can call max with the list and the key argument set to a function that returns the value that we want to sort by.

Categories
Python Answers

How to convert a PIL Image into a numpy array with Python?

Sometimes, we want to convert a PIL Image into a numpy array with Python.

In this article, we’ll look at how to convert a PIL Image into a numpy array with Python.

How to convert a PIL Image into a numpy array with Python?

To convert a PIL Image into a numpy array with Python, we call numpy.array with the PIL image.

For instance, we write

pix = numpy.array(pic)

to call numpy.array with the pic PIL image object to return an array with the pixel data of pic.

Conclusion

To convert a PIL Image into a numpy array with Python, we call numpy.array with the PIL image.

Categories
Python Answers

How to use StringIO in Python?

Sometimes, we want to use StringIO in Python.

In this article, we’ll look at how to use StringIO in Python.

How to use StringIO in Python?

To use StringIO in Python, we can import it from the io module.

For instance, we write

from io import StringIO

to import StringIO from the io module.

Conclusion

To use StringIO in Python, we can import it from the io module.