0

I have the following question. As input I have a datename, which must have a certain nomenclature, like

  1. Footballfield_01_CampNou_108m_Length_68.5_Width
  2. Footballfield_02_Bernabeau_105m_Length_68.5_Width

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?

  • try, `data = dataname.split("_")`. this will give you a list of fields. Assuming you have a standard nomenclature, `data[3]` will be the length, and `data[5]` will be the width. You may have to clean-up the `m` units for lengths before you actually perform any area-related operations – rite2hhh Oct 12 '20 at 17:14
  • Does this answer your question? [Extract int digits and float number from a python string](https://stackoverflow.com/questions/40840685/extract-int-digits-and-float-number-from-a-python-string) – Tomerikoo Oct 12 '20 at 17:16

1 Answers1

1

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 numbers

https://regex101.com/r/8BOvMh/1 example working online

Ryan Schaefer
  • 3,047
  • 1
  • 26
  • 46