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, we can use the random module’s randrange function.

For instance, we write:

from random import randrange
print(randrange(10))

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

Conclusion

To generate random integers between 0 and 9 with Python, we can use the random module’s randrange function.

Categories
Python Answers

How to generate a random string with upper case letters and digits with Python?

Sometimes, we want to generate a random string with upper case letters and digits with Python.

In this article, we’ll look at how to generate a random string with upper case letters and digits with Python.

How to generate a random string with upper case letters and digits with Python?

To generate a random string with upper case letters and digits with Python, we can use the random.choices method.

For instance, we write:

import random
import string

N = 10

gen = ''.join(random.choices(string.ascii_uppercase + string.digits, k=N))
print(gen)

to generate a 10 character random string.

We call join on an empty string with the randomly generated characters.

We generate 10 random characters with random.choices(string.ascii_uppercase + string.digits, k=N).

string.ascii_uppercase + string.digits will generate numbers and letter characters.

k is the number of characters to generate.

And an array characters is returned.

Therefore, gen should be something like 'M5HHZYW06X'.

random.choices is available since Python 3.6.

Conclusion

To generate a random string with upper case letters and digits with Python, we can use the random.choices method.

Categories
Python Answers

How to extract extension from filename in Python?

Sometimes, we want to extract extension from filename in Python.

In this article, we’ll look at how to extract extension from filename in Python.

How to extract extension from filename in Python?

To extract extension from filename in Python, we can use the os.path.splittext method.

For instance, we write:

import os

filename, file_extension = os.path.splitext('/path/to/somefile.ext')
print(file_extension)

We call os.path.splittext with the path we want to extract the file extension from.

Then we get the file_extension from the 2nd entry in the returned tuple.

Therefore, file_extension is '.ext'.

Conclusion

To extract extension from filename in Python, we can use the os.path.splittext method.

Categories
Python Answers

How to convert two lists into a dictionary with Python?

Sometimes, we want to convert two lists into a dictionary with Python.

In this article, we’ll look at how to convert two lists into a dictionary with Python.

How to convert two lists into a dictionary with Python?

To convert two lists into a dictionary with Python, we can use the dict and zip functions.

For instance, we write:

keys = ['a', 'b', 'c']
values = [1, 2, 3]
dictionary = dict(zip(keys, values))
print(dictionary)

We call zip with the keys and values to combine the entries of each into tuples.

Then we call dict to turn each tuple into an entry in the dictionary.

Therefore, dictionary is {'a': 1, 'b': 2, 'c': 3}.

Conclusion

To convert two lists into a dictionary with Python, we can use the dict and zip functions.

Categories
Python Answers

How to parse XML and count instances of a particular node attribute with Python?

Sometimes, we want to parse XML and count instances of a particular node attribute with Python.

In this article, we’ll look at how to parse XML and count instances of a particular node attribute with Python.

How to parse XML and count instances of a particular node attribute with Python?

To parse XML and count instances of a particular node attribute with Python, we can use the xml.etree.ElementTree constructor.

For instance, we write:

file.xml

<foo>
   <bar>
      <type foobar="1"/>
      <type foobar="2"/>
   </bar>
</foo>

to define an XML file with some content.

Then we write:

main.py:

import xml.etree.ElementTree as ET

root = ET.parse('file.xml').getroot()

for type_tag in root.findall('bar/type'):
    value = type_tag.get('foobar')
    print(value)

to import the xml.etree.ElementTree constructor as ET.

Then we use the ET constructor with the XML file path.

Next, we call getRoot to get the root node of the XML file.

Then we call findall with the node tag path we’re looking for.

We then loop through the returned nodes with a for loop.

And then we call get with the attribute name string of the attribute we want to get.

This returns the attribute value and we assign that to value and print it.

Therefore, we see:

1
2

printed.

Conclusion

To parse XML and count instances of a particular node attribute with Python, we can use the xml.etree.ElementTree constructor.