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.

Categories
Python Answers

How to flatten a shallow list in Python?

Sometimes, we want to flatten a shallow list in Python.

In this article, we’ll look at how to flatten a shallow list in Python.

How to flatten a shallow list in Python?

To flatten a shallow list in Python, we can use the itertools.chain method.

For instance, we write:

import itertools

list_of_menuitems = [['image00', 'image01'], ['image10'], []]

chain = itertools.chain(*list_of_menuitems)
print(list(chain))

We defined the list_of_menuitems list which has lists inside it.

Then we call itertools.chain with the list_of_menuitems used as arguments since we spread it with *.

And then we convert the returned chain iterator to a list with list.

Therefore, we see:

['image00', 'image01', 'image10']

printed.

Conclusion

To flatten a shallow list in Python, we can use the itertools.chain method.

Categories
Python Answers

How to keep dictionary keys/values in same order as declared with Python?

Sometimes, we want to keep dictionary keys/values in same order as declared with Python.

In this article, we’ll look at how to keep dictionary keys/values in same order as declared with Python.

How to keep dictionary keys/values in same order as declared with Python?

To keep dictionary keys/values in same order as declared with Python, we can use the OrderDict class to create our dictionary.

For instance, we write:

from collections import OrderedDict

d = {'ac': 33, 'gw': 20, 'ap': 102, 'za': 321, 'bs': 10}
ordered_d = OrderedDict(d)
print(ordered_d)

We import the OrderedDict class from the collections module.

Then we declared a regular dictionary and assigned it to d.

Next, we use d as the argument of OrderedDict and assigned it to ordered_d.

Therefore, ordered_d is:

OrderedDict([('ac', 33), ('gw', 20), ('ap', 102), ('za', 321), ('bs', 10)])

Conclusion

To keep dictionary keys/values in same order as declared with Python, we can use the OrderDict class to create our dictionary.