Reputation: 2699
I'm trying to split a filename use it by parts.
The file is always an apache binary (for example, httpd-2.2.31.tar.gz) and I only need the version (in this case 22) in that specific format.
I have done it and it works, but I believe there should be a better way
binary_name = "httpd-2.2.31.tar.gz"
app, ver_tar = binary_name.split("-")
version = ver_tar.split(".")
print ("Version %s%s" % (version[0], version[1]))
Is it possible to do it in a better way?
Upvotes: 0
Views: 162
Reputation: 2665
As far as a better way there is no such thing as long as your code solves the problem you set out to solve, and in this case it does. As far as performance goes there are probably faster ways to achieve the same thing but even with a list of thousands of string to parse the difference in speed would be minimal or indistinguishable. Therefore I assume you are instead asking for a different way to accomplish the same goal, so I have written a small function and several test segments. The function gives slightly more control.
def get_version(file_name, accuracy=0, sep=None):
'''
:string file_name is the string you want to parse
:int accuracy is the length of the output format
1 = 1 of 1.23.3
2 = 1.2 of 1.23.3
3 = 1.23 of 1.23.3
0 = 1.23.3 or full length
:string sep, is the string seperator you wish to use, 1.1, 1_1, 11 etc...
'''
if not sep:
sep = ''
data = file_name.split('.')
str_ver = data[0:-2]
ver = []
for i in str_ver:
if len(ver) < accuracy or accuracy == 0:
try:
if len(i)>1:
n = ''
for x in i:
try:
n+=str(int(x))
except:
pass
else:
n = str(int(i))
ver.append(n)
except:
pass
return sep.join(ver)
print get_version("httpd-2.2.31.tar.gz", 1, '--')
print get_version("httpd-2.2.31.tar.gz", 2, '::')
print get_version("httpd-2.2.31.tar.gz", 3, '_')
print get_version("httpd-2.2.31.tar.gz", 2)#what you were asking for
print get_version("httpd-2.2.31.tar.gz")
Upvotes: 0
Reputation: 67988
use re
(?<=-)(\d+)\.(\d+)
See demo.
or
import re
x= "httpd-2.2.31.tar.gz"
print re.findall(r"(?<=-)(\d+)\.(\d+)", x)
Upvotes: 2
Reputation: 2162
You could make it a little easier by using regular expressions. There's a nifty site called Pythex where you can experiment with these yourself. To fix your example:
import re
r = re.compile(".*-(\d+\.\d+\.\d+).tar.gz")
print(r.match("httpd-2.2.21.tar.gz").groups()[0])
Upvotes: 0
Reputation: 482
if you want to do it in one line,may be it will help :
binary_name = "httpd-2.2.31.tar.gz"
print binary_name.split("-")[1][:3].replace('.','')
will return 22
Upvotes: 1