Categories
Python Answers

How to get string objects instead of Unicode from JSON with Python?

Sometimes, we want to get string objects instead of Unicode from JSON with Python.

In this article, we’ll look at how to get string objects instead of Unicode from JSON with Python.

How to get string objects instead of Unicode from JSON with Python?

To get string objects instead of Unicode from JSON with Python, we can use the encode method.

For instance, we write

nl = json.loads(js)
nl = [s.encode('utf-8') for s in nl]

to call json.loads to load the js JSON string array string into the nl list.

Then we loop through the nl list to call encode with 'utf-8' on each string in the nl list to convert them to byte strings and put the converted strings in a new list.

Finally, we assign the returned list to `nl.

Conclusion

To get string objects instead of Unicode from JSON with Python, we can use the encode method.

Categories
Python Answers

How to fix ImportError: No module named requests with Python?

Sometimes, we want to fix ImportError: No module named requests with Python.

In this article, we’ll look at how to fix ImportError: No module named requests with Python.

How to fix ImportError: No module named requests with Python?

To fix ImportError: No module named requests with Python, we’ve to install the requests library.

To install it, we run

pip install requests

Then we can import the library in our Python scripts.

Conclusion

To fix ImportError: No module named requests with Python, we’ve to install the requests library.

Categories
Python Answers

How to manage local vs production settings in Python Django?

Sometimes, we want to manage local vs production settings in Python Django.

In this article, we’ll look at how to manage local vs production settings in Python Django.

How to manage local vs production settings in Python Django?

To manage local vs production settings in Python Django, we can use the --settings flag to specify the settings file to use.

We create a settings file for each environment.

And then we can run our Django app with the settings for the given environment with

python3 manage.py runserver --settings=settings_you_wish_to_use.py

to run runserver with

--settings=settings_you_wish_to_use.py

to run our app with the settings in settings_you_wish_to_use.py.

Conclusio

To manage local vs production settings in Python Django, we can use the --settings flag to specify the settings file to use.

Categories
Python Answers

How to get a directory-tree listing in Python?

Sometimes, we want to get a directory-tree listing in Python.

In this article, we’ll look at how to get a directory-tree listing in Python.

How to get a directory-tree listing in Python?

To get a directory-tree listing in Python, we can use the os.walk method.

For instance, we write

import os

for dirname, dirnames, filenames in os.walk('.'):
    for subdirname in dirnames:
        print(os.path.join(dirname, subdirname))

    for filename in filenames:
        print(os.path.join(dirname, filename))

to call os.walk with '.' to return an iterator with the directories and files in the folder.

It traverses the whole directory tree and returns items from all levels.

In the loop, we print out all the child directories with

for subdirname in dirnames:
    print(os.path.join(dirname, subdirname))

And we print out all the child file paths with

for filename in filenames:
    print(os.path.join(dirname, filename))

Conclusion

To get a directory-tree listing in Python, we can use the os.walk method.

Categories
Python Answers

How to convert integer to string in Python?

Sometimes, we want to convert integer to string in Python.

In this article, we’ll look at how to convert integer to string in Python.

How to convert integer to string in Python?

To convert integer to string in Python, we can use the str function.

For instance, we write

s = str(i)

to convert integer i into a string with str.

Conclusion

To convert integer to string in Python, we can use the str function.