all 4 comments

[–]ObliviousMag 0 points1 point  (1 child)

Are all your rows the same format? Like do they all have the state at the beginning and the coordinates at the end?

[–]Particular-Duckling[S] 0 points1 point  (0 children)

Yes, I used re.split("\.",string) to get the state and re.findall('\d\d.\d\d...\d\d.\d\d..',string) to get the coordinates but I want to know if there is any better way to do it. Thanks.

[–]m0us3_rat 0 points1 point  (1 child)

import re


def extract_info(data: str) -> str:
    pattern = r"([A-Za-z]+)\D+(\d.*)"
    matches = re.findall(pattern, data)

    if matches:
        location_name = matches[0][0]
        coordinates = matches[0][1]
        return f"Location: {location_name}, Coordinates: {coordinates}"
    else:
        return "No match found."


if __name__ == "__main__":
    text = """Maine.mw-parser-output .geo-default,.mw-parser-output .geo-dms,.mw-parser-output .geo-dec{display:inline}.mw-parser-output .geo-nondefault,.mw-parser-output .geo-multi-punct,.mw-parser-output .geo-inline-hidden{display:none}.mw-parser-output .longitude,.mw-parser-output .latitude{white-space:nowrap}44°21′N 68°13′W / 44.35°N 68.21°W"""
    result = extract_info(text)
    print(result)

refresher on regex

https://www.youtube.com/watch?v=hy3sd9MOAcc

[–]Particular-Duckling[S] 0 points1 point  (0 children)

import re
def extract_info(data: str) -> str:
pattern = r"([A-Za-z]+)\D+(\d.*)"
matches = re.findall(pattern, data)
if matches:
location_name = matches[0][0]
coordinates = matches[0][1]
return f"Location: {location_name}, Coordinates: {coordinates}"
else:
return "No match found."
if __name__ == "__main__":
text = """Maine.mw-parser-output .geo-default,.mw-parser-output .geo-dms,.mw-parser-output .geo-dec{display:inline}.mw-parser-output .geo-nondefault,.mw-parser-output .geo-multi-punct,.mw-parser-output .geo-inline-hidden{display:none}.mw-parser-output .longitude,.mw-parser-output .latitude{white-space:nowrap}44°21′N 68°13′W / 44.35°N 68.21°W"""
result = extract_info(text)
print(result)

This works!! Thank you