The string variable myvar
can have the following values:
261.30 (NM) / 300.76 (MI) / 483.93 (KM)
952.27 (NM) / 1,096.09 (MI) / 1,763.61 (KM)
I need to extract the numbers 483.93
and 1,763.61
. These numbers should be converted to float numbers or rounded up to integers.
This is what I tried:
mylar = "261.30 (NM) / 300.76 (MI) / 483.93 (KM)"
int(myvar[28:-8])
It works in the first case ( 261.30 (NM) / 300.76 (MI) / 483.93 (KM)
). But it fails in case of 952.27 (NM) / 1,096.09 (MI) / 1,763.61 (KM)
. Is there any generic solution?
You can use this regex that detects the last number in the string and places it in group1,
([^ ]*)[^\d]*$
Then with following python code, you can convert it into float or int. I am converting it to float as they are decimal values but you can easily cast them to int.
import re
arr = ['261.30 (NM) / 300.76 (MI) / 483.93 (KM)','952.27 (NM) / 1,096.09 (MI) / 1,763.61 (KM)']
for s in arr:
val = re.search(r'([^ ]*)[^\d]*$', s)
floatval = float(re.sub(r',','',val.group(1)))
print('Float value: ' + str(floatval))
print('Int value: ' + str(int(floatval)))
Prints,
Float value: 483.93
Int value: 483
Float value: 1763.61
Int value: 1763
With single re.search
function, without any replacement:
import re
myvar = '952.27 (NM) / 1,096.09 (MI) / 1,763.61 (KM)'
num = re.search(r'\/\s+(\d+),?(\d+\.\d+)\s+\(KM\)', myvar)
if num:
num = float(num.group(1) + num.group(2))
print(num)
The output:
1763.61
You could also achieve the same by using only split()
str1="261.30 (NM) / 300.76 (MI) / 483.93 (KM)"
last_num_string=str1.split('/')[2].split('(')[0]
last_num_float=float(last_num_string)
print(last_num_float)
What I would do in this case is create a list of the values, from which I can extract any of those.
myvar2 = myvar.replace("/", "").replace(",", "")
myvar2 = myvar2.split()
# This gives a list like this: ["261.30","(NM)","300.76","(MI)","483.93","(KM)"]
myfloat = float(myvar[4]) # This will return 483.93 on the first string and 1,763.61 on the second.
You can do it with regular expression.
import re
target = '952.27 (NM) / 1,096.09 (MI) / 1,763.61 (KM)'
regex = r'.* \(NM\) \/ .* \(MI\) \/ (.*) \(KM\)'
res_str = re.findall(regex, target)
float_str = res_str[0].replace(',', '')
result = float(float_str)
import re
m="261.30 (NM) / 300.76 (MI) / 483.93 (KM)"
print(float(re.split('\/',re.sub(',','',m))[2][:-5]))
Since the values are followed by (KM)
you could use a positive lookahead:
\S+(?= \(KM\))
\\S+
Match 1+ non whitespace characters (?= \\(KM\\))
Positive lookahead to check what is on the right is a space and (KM) For example:
regex = r"\S+(?= \(KM\))"
strings = ["261.30 (NM) / 300.76 (MI) / 483.93 (KM)", "952.27 (NM) / 1,096.09 (MI) / 1,763.61 (KM)"]
for s in strings:
matches = re.search(regex, s)
if matches:
print(float(matches.group().replace(',', '')))
That will give you:
483.93
1763.61
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.