Categories
Python Answers

How to ignore ‘Incorrect padding’ error when base64 decoding with Python?

Sometimes, we want to ignore ‘Incorrect padding’ error when base64 decoding with Python.

In this article, we’ll look at how to ignore ‘Incorrect padding’ error when base64 decoding with Python.

How to ignore ‘Incorrect padding’ error when base64 decoding with Python?

To ignore ‘Incorrect padding’ error when base64 decoding with Python, we add the padding to the base64 string.

For instance, we write

base64.b64decode(s + b'==')

to call b64decode with the base64 string s concatenated with the padding string to decode the string without errors.

Conclusion

To ignore ‘Incorrect padding’ error when base64 decoding with Python, we add the padding to the base64 string.

Categories
Python Answers

How to count frequency of words in a list and sort by frequency with Python?

Sometimes, we want to count frequency of words in a list and sort by frequency with Python.

In this article, we’ll look at how to count frequency of words in a list and sort by frequency with Python.

How to count frequency of words in a list and sort by frequency with Python?

To count frequency of words in a list and sort by frequency with Python, we can use the Counter class.

For instance, we write

from collections import Counter
list1 = ['apple','egg','apple','banana','egg','apple']
counts = Counter(list1)
print(counts)

to create a Counter object with list1.

Then we get a Counter object that has the key being the item in list1 and the value being the frequency of each item.

The items are sorted by frequency in counts.

Conclusion

To count frequency of words in a list and sort by frequency with Python, we can use the Counter class.

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.