Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save guillaumepiot/4539986 to your computer and use it in GitHub Desktop.
Save guillaumepiot/4539986 to your computer and use it in GitHub Desktop.
PYTHON - Convert URL in text to anchor tags (links)
@endofline
Copy link

note: running this on a string that already contains anchors will wrap the links inside the anchors again

@Shanushivam
Copy link

import json
from datetime import datetime

def convert_iso_to_millis(iso_str):
# Convert ISO 8601 string to milliseconds since epoch
dt = datetime.strptime(iso_str, "%Y-%m-%dT%H:%M:%SZ")
return int(dt.timestamp() * 1000)

def load_json(filename):
with open(filename, "r") as file:
return json.load(file)

def save_json(data, filename):
with open(filename, "w") as file:
json.dump(data, file, indent=2)

IMPLEMENT: Convert data from data-1.json and data-2.json into unified format

def combine_telemetry_data():
data1 = load_json("data-1.json") # Already in milliseconds
data2 = load_json("data-2.json") # ISO timestamps

result = []

# Format data1
for entry in data1:
    result.append({
        "timestamp": entry["timestamp"],
        "device_id": entry["device_id"],
        "value": entry["value"]
    })

# Format data2 and convert timestamps
for entry in data2:
    timestamp_ms = convert_iso_to_millis(entry["timestamp"])
    result.append({
        "timestamp": timestamp_ms,
        "device_id": entry["device_id"],
        "value": entry["value"]
    })

# Optional: sort by timestamp
result.sort(key=lambda x: x["timestamp"])

return result

IMPLEMENT: Entry point for the program

def main():
combined_data = combine_telemetry_data()
save_json(combined_data, "output.json")

if name == "main":
main()

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment