We can utilize python parse version strings with the version-parser python package. Actually, the version string is a bit different from an ordinary python string. Especially string comparing is completely different here. In most of the DevOps-related operations, we have to compare the current system/ module version. On the basis of the existing version, we have to manage the control flow. Anyways in this article, we are going to demonstrate step-wise parsing version string.
Python parse version strings ( Solutions ) –
There are two approaches to fix this issue. One is simple and systematic because here we will use the third-party library and the second one is the custom method.
Solution 1: Using version-parser –
The first thing to use this version-parser is to install the version-parser python module. We can use the pip package manager to install the version-parser. Here is the command for the same.
pip install version-parser
In order to use this package, we need to import first, and then we need to create two version string objects. Let’s see the code first.
from version_parser.version import Version v1 = Version("v3.1.4") v2 = Version("v3.3.2") print(v1 < v2) print(v1 > v2) print(v1 == v2)
Here we have created v1 and v2 as string python version string. Now we can easily perform all the comparing.
Solution 2: Custom Solution –
In this part, we have to custom write the condition on the basis of string format. We can split our string on the basis of the separator into two or more lists. Take the below code for reference.
Now we need to iterate both the list and compare the list element and give the preference to the left or initial orders. we have to keep iterating till we get the difference.
The limitation of this approach is we need to write different logic if the format is changed.
Data Science Learner Team
Join our list
Subscribe to our mailing list and get interesting stuff and updates to your email inbox.