Categories
Python Answers

How to import a file from a subdirectory with Python?

Sometimes, we want to import a file from a subdirectory with Python.

In this article, we’ll look at how to import a file from a subdirectory with Python.

How to import a file from a subdirectory with Python?

To import a file from a subdirectory with Python, we can add an __init__.py file into the root of the subdirectory.

Then we can import the modules from the subdirectory.

We can leave the file empty.

This will mark the subdirectory as a directory that we can import modules from.

Conclusion

To import a file from a subdirectory with Python, we can add an __init__.py file into the root of the subdirectory.

Categories
Python Answers

How to get list of values from dict with Python?

Sometimes, we want to get list of values from dict with Python.

In this article, we’ll look at how to get list of values from dict with Python.

How to get list of values from dict with Python?

To get list of values from dict with Python, we can use the values method.

For instance, we write

list(d.values())

to call d.values to get the values of the dict d in an iterator.

Then we call list to convert the iterator to a list.

Conclusion

To get list of values from dict with Python, we can use the values method.

Categories
Python Answers

How to extract text from a PDF file with Python?

Sometimes, we want to extract text from a PDF file with Python

In this article, we’ll look at how to extract text from a PDF file with Python.

How to extract text from a PDF file with Python?

To extract text from a PDF file with Python, we can use the tika package.

To install it, we run

pip install tika

Then we use it by writing

from tika import parser

raw = parser.from_file('sample.pdf')
print(raw['content'])

to call parser.from_file with the PDF file path to read the PDF file.

And then we get the content with the 'content' key from the raw dict.

Conclusion

To extract text from a PDF file with Python, we can use the tika package.

Categories
Python Answers

How to generate random integers between 0 and 9 with Python?

Sometimes, we want to generate random integers between 0 and 9 with Python.

In this article, we’ll look at how to generate random integers between 0 and 9 with Python.

How to generate random integers between 0 and 9 with Python?

To generate random integers between 0 and 9 with Python, w ecan use the random.randrange method.

For instance, we write

from random import randrange

print(randrange(10))

to call randrange with to 10 to generate a random number between 0 and 9.

Conclusion

To generate random integers between 0 and 9 with Python, w ecan use the random.randrange method.

Categories
Python Answers

How to delete a list element by value with Python?

Sometimes, we want to delete a list element by value with Python.

In this article, we’ll look at how to delete a list element by value with Python.

How to delete a list element by value with Python?

To delete a list element by value with Python, we can use the remove method.

For instance, we write

xs = ["a", "b", "c", "d"]
xs.remove("b")

to call xs.remove with ‘b' to remove 'b' from the xs list.

Conclusion

To delete a list element by value with Python, we can use the remove method.