Skip to content

Instantly share code, notes, and snippets.

@Youssef-Harby
Created August 21, 2023 09:43
Show Gist options
  • Save Youssef-Harby/3ce6022023ccf042357cef35732c0595 to your computer and use it in GitHub Desktop.
Save Youssef-Harby/3ce6022023ccf042357cef35732c0595 to your computer and use it in GitHub Desktop.
How to extract X-Long and Y-Lat from google maps url
import csv
import re
def extract_lat_long(csv_file, url_column):
pattern = r".+!3d(-?\d+\.\d+)!4d(-?\d+\.\d+).+"
data = []
# Read the CSV file and extract latitude and longitude values
with open(csv_file, 'r') as f:
reader = csv.DictReader(f)
for row in reader:
url = row[url_column]
match = re.search(pattern, url)
if match:
lat = match.group(1)
lng = match.group(2)
row["latitude"] = lat
row["longitude"] = lng
data.append(row)
# Write the updated data back to the CSV file
with open(csv_file, 'w', newline='') as f:
fieldnames = reader.fieldnames + ["latitude", "longitude"]
writer = csv.DictWriter(f, fieldnames=fieldnames)
writer.writeheader()
for row in data:
writer.writerow(row)
# Usage:
csv_file = "recording_studio.csv"
url_column = "hfpxzc href"
extract_lat_long(csv_file, url_column)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment