Reputation:
I have the following question. As input I have a datename, which must have a certain nomenclature, like
For the width the meter is missing. But I cannot change the name and have to work with this. My task now is to write a code, which extracts only the length and the width from the data name to calculate the area on it. I have had several ideas, but these are error-prone and not robust enough. My idea now is based on knowing that I have an integer value (length) and a float value (width). I want to write a code that finds the float value and the integer value in the string and assigns them to the width and length but I can't find any way, can anyone help me?
Upvotes: 0
Views: 256
Reputation: 3120
Seems like you could use regex to solve this task:
import re
//somehow get the input_list
for entry in input_list:
match = re.match(r"\w+_\d+_\w+_(\d+)m_Length_(([0-9]*[.])?[0-9]+)_Width", entry)
length, width, _ = match.groups()
length = int(length)
width = float(width)
going into more detail about the regex:
\w+
matches alphabetical characters\d+
matches digits_
matches _
literally()
around a some regex captures that value to return(([0-9]*[.])?[0-9]+)
is caputuring the floating point part as explained here Regular expression for floating point numbershttps://regex101.com/r/8BOvMh/1 example working online
Upvotes: 1