I am trying to do the following:
Find out all the numerical values in the string.
input_string = "高露潔光感白輕悅薄荷牙膏100 79.80"
numbers = re.finditer(r'[-+]?[0-9]*\.?[0-9]+(?:[eE][-+]?[0-9]+)?',input_string)
for number in numbers:
print ("{} start > {}, end > {}".format(number.group(), number.start(0), number.end(0)))
'''Output'''
>>100 start > 12, end > 15
>>79.80 start > 18, end > 23
And then I want to replace the entire integer and float value with a specific format:
INT_(number of digit) and FLT(number of decimal places)
eg. 100 -> INT_3 // 79.80 -> FLT_2
So the wait output line looks like this:
"高露潔光感白輕悅薄荷牙膏INT_3 FLT2"
But the string replacing the substring method in Python looks weird, and I can't archive what I want to do.
So I'm trying to use a substring that adds substring methods
string[:number.start(0)] + "INT_%s"%len(number.group()) +.....
which looks stupid and, most importantly, I still can't get it to work.
Can someone give me some advice on this issue?
source
share