Categories
Python Answers

How to convert int to bytes in Python?

Sometimes, we want to convert int to bytes in Python.

In this article, we’ll look at how to convert int to bytes in Python.

How to convert int to bytes in Python?

To convert int to bytes in Python, we can use the to_bytes method.

For instance, we write

b = (1024).to_bytes(2, byteorder='big')

to convert int 1024 to a byte string with to_bytes called with 2.

We specify that the byte string is big endian by setting byteorder to 'big'.

Conclusion

To convert int to bytes in Python, we can use the to_bytes method.

Categories
Python Answers

How to get a random value from dictionary with Python?

Sometimes, we want to get a random value from dictionary with Python.

In this article, we’ll look at how to get a random value from dictionary with Python.

How to get a random value from dictionary with Python?

To get a random value from dictionary with Python, we can use the random.choice method.

For instance, we write

import random

d = {'VENEZUELA':'CARACAS', 'CANADA':'OTTAWA'}
c = random.choice(list(d.values()))

to call random_choice with the list of dict values that we get from dict d with list(d.values()) to return a random dict value from d.

Conclusion

To get a random value from dictionary with Python, we can use the random.choice method.

Categories
Python Answers

How to format output string with right alignment with Python?

Sometimes, we want to format output string with right alignment with Python.

In this article, we’ll look at how to format output string with right alignment with Python.

How to format output string with right alignment with Python?

To format output string with right alignment with Python, we can use the string format method.

For instance, we write

line_new = '{:>12}  {:>12}  {:>12}'.format(word[0], word[1], word[2])

to call '{:>12} {:>12} {:>12}'.format with the 3 items to put in place of the placeholders.

The placeholders specify that the width of each column is 12 characters and that the alignment of each item is right.

Conclusion

To format output string with right alignment with Python, we can use the string format method.

Categories
Python Answers

How to execute Python script via crontab?

Sometimes, we want to execute Python script via crontab.

In this article, we’ll look at how to execute Python script via crontab.

How to execute Python script via crontab?

To execute Python script via crontab, we can edit the crontab file to add an entry to run the script.

For instance, we add

*/10 * * * * /usr/bin/python script.py

to run script.py every 10 minutes.

Conclusion

To execute Python script via crontab, we can edit the crontab file to add an entry to run the script.

Categories
Python Answers

How to remove stop words using nltk or Python?

Sometimes, we want to remove stop words using nltk or Python

In this article, we’ll look at how to remove stop words using nltk or Python.

How to remove stop words using nltk or Python?

To remove stop words using nltk or Python, we can use the stopwords.words list from nltk.

For instance, we write

from nltk.corpus import stopwords
# ...
filtered_words = [word for word in word_list if word not in stopwords.words('english')]

to get English stop words with stopwords.words('english').

Then we return a list of words in word_list that aren’t stop words with

word for word in word_list if word not in stopwords.words('english')

Conclusion

To remove stop words using nltk or Python, we can use the stopwords.words list from nltk.