Created
July 16, 2024 10:29
-
-
Save bencleary/d9f4f2ca7752540cea369a227a336075 to your computer and use it in GitHub Desktop.
Used for generating mock CSV's and JSON data for interview questions
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import csv | |
import json | |
from datetime import datetime, timedelta | |
# Function to generate sensor data | |
def generate_sensor_data(num_rows: int): | |
sensor_data = [] | |
base_time = datetime(2024, 7, 1, 0, 0) | |
for i in range(num_rows): | |
timestamp = base_time + timedelta(hours=i) | |
sensor_id = i + 1 | |
temperature = round(20 + 5 * (i % 2) + 0.1 * i, 1) | |
humidity = 40 + (i % 10) | |
pressure = 1000 + (i % 15) | |
sensor_data.append({ | |
"Timestamp": timestamp.isoformat() + "Z", | |
"Sensor ID": sensor_id, | |
"Temperature": temperature, | |
"Humidity": humidity, | |
"Pressure": pressure | |
}) | |
return sensor_data | |
# Generate sensor data | |
data = generate_sensor_data(25) | |
# Write to CSV | |
csv_file = 'sensor_readings.csv' | |
with open(csv_file, 'w', newline='') as file: | |
writer = csv.DictWriter(file, fieldnames=data[0].keys()) | |
writer.writeheader() | |
writer.writerows(data) | |
# Write to JSON | |
json_file = 'sensor_readings.json' | |
with open(json_file, 'w') as file: | |
json.dump(data, file, indent=4) | |
print(f"CSV and JSON files have been generated: {csv_file}, {json_file}") |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment