Categories
Python Answers

How to turn a Python datetime into a string, with readable format date?

Sometimes, we want to turn a Python datetime into a string, with readable format date.

In this article, we’ll look at how to turn a Python datetime into a string, with readable format date.

How to turn a Python datetime into a string, with readable format date?

To turn a Python datetime into a string, with readable format date, we can use the strftime method.

For instance, we write

d = my_datetime.strftime("%B %d, %Y")

to call my_datetime.strftime with the date format string to return a date formatted with the month, date and year from the my_datetime datetime object.

Conclusion

To turn a Python datetime into a string, with readable format date, we can use the strftime method.

Categories
Python Answers

How to extract multiple JSON objects from one file with Python?

Sometimes, we want to extract multiple JSON objects from one file with Python.

In this article, we’ll look at how to extract multiple JSON objects from one file with Python.

How to extract multiple JSON objects from one file with Python?

To extract multiple JSON objects from one file with Python, we put the JSON objects in a JSON array.

Then we call json.load to parse the content of the JSON file.

For instance, we write

[
  {
    "ID": "12345",
    "Timestamp": "20220101",
    "Usefulness": "Yes",
    "Code": [{ "event1": "A", "result": "1" }]
  },
  {
    "ID": "1A35B",
    "Timestamp": "20220102",
    "Usefulness": "No",
    "Code": [{ "event1": "B", "result": "1" }]
  },
  {
    "ID": "AA356",
    "Timestamp": "20220103",
    "Usefulness": "No",
    "Code": [{ "event1": "B", "result": "0" }]
  }
]

in file.json.

Then we open the file and parse the file into a list of dicts with

import json

with open('file.json') as json_file:
    data = json.load(json_file)

We call open to open file.json.

And then we call json.load to parse the json_file content into a list of dicts with the JSON objects.

Conclusion

To extract multiple JSON objects from one file with Python, we put the JSON objects in a JSON array.

Then we call json.load to parse the content of the JSON file.

Categories
Python Answers

How to check whether a number is divisible by another number with Python?

Sometimes, we want to check whether a number is divisible by another number with Python.

In this article, we’ll look at how to check whether a number is divisible by another number with Python.

How to check whether a number is divisible by another number with Python?

To check whether a number is divisible by another number with Python, we can use the modulo operator.

For instance, we write

n % k == 0

to check if n is evenly divisible by k.

Conclusion

To check whether a number is divisible by another number with Python, we can use the modulo operator.

Categories
Python Answers

How to fix Python Django URLs TypeError: view must be a callable or a list/tuple in the case of include()?

Sometimes, we want to fix Python Django URLs TypeError: view must be a callable or a list/tuple in the case of include().

In this article, we’ll look at how to fix Python Django URLs TypeError: view must be a callable or a list/tuple in the case of include().

How to fix Python Django URLs TypeError: view must be a callable or a list/tuple in the case of include()?

To fix Python Django URLs TypeError: view must be a callable or a list/tuple in the case of include(), we can reference the view with the name in the urlpatterns list in urls.py and the view function.

For instance, we write

from django.conf.urls import include, url

from django.contrib.auth.views import login
from myapp.views import home, contact

urlpatterns = [
    url(r'^$', home, name='home'),
    url(r'^contact/$', contact, name='contact'),
    url(r'^login/$', login, name='login'),
]

to assign urlpatterns to a list that has url objects created by calling url with the view function as the 2nd argument and the name of the view that’s mapped to the URL pattern as the 3rd argument.

Then the URLs will be mapped to the views in the 2nd argument.

Conclusion

To fix Python Django URLs TypeError: view must be a callable or a list/tuple in the case of include(), we can reference the view with the name in the urlpatterns list in urls.py and the view function.

Categories
Python Answers

How to escape os.system() calls with Python?

Sometimes, we want to escape os.system() calls with Python.

In this article, we’ll look at how to escape os.system() calls with Python.

How to escape os.system() calls with Python?

To escape os.system() calls with Python, we can use shlex.quote.

For instance, we write

from shlex import quote
command = 'ls -l {}'.format(quote(filename))

to call quote with filename to escape the filename path.

shlex.quote can be used with Linux only.

Conclusion

To escape os.system() calls with Python, we can use shlex.quote.