forked from matthewrenze/dark-sky-weather-scripts
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Convert.py
74 lines (54 loc) · 1.97 KB
/
Convert.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
#%% Convert Dark Sky JSON to CSV
# Purpose: Converts Dark Sky JSON files to a CSV file
# Author: Matthew Renze
# Usage: python.exe Convert.py input-folder output-file
# - input-folder = the folder containing Dark Sky JSON files
# - output-file = the CSV file to write the Dark SKY data to
# Example: python.exe Convert.py C:\InputFolder\ C:\OutputFile
#%% Load libraries
import sys
import os
import json
import csv
#%% Get console arguments
input_folder_path = sys.argv[1]
output_file_path = sys.argv[2]
#%% Create target CSV file
output_file = open(
file = output_file_path,
mode = "wt",
newline = "")
#%% Create a csv writer
csv_writer = csv.writer(output_file)
#%% Write header to file
csv_writer.writerow(["Date", "Latitude", "Longitude", "Time Zone", "Offset", "Humidity", "Temperature Low", "Temperature High"])
#%% Get list of files in source folder
input_files = os.listdir(input_folder_path)
#%% Process each source file
for input_file_name in input_files:
# Create source file path
source_file_path = os.path.join(
input_folder_path,
input_file_name)
# Open the source file
source_file = open(source_file_path)
# Load the JSON data
json_data = json.load(source_file)
# Close the source file
source_file.close()
# Get fields from JSON data
date = input_file_name.replace(".json", "")
latitude = json_data["latitude"]
longitude = json_data["longitude"]
time_zone = json_data["timezone"]
offset = json_data["offset"]
# Get daily data from JSON data
daily_data = json_data["daily"]["data"][0]
# Get fields from daily data
humidity = daily_data["humidity"]
temperature_low = daily_data["temperatureLow"]
temperature_high = daily_data["temperatureHigh"]
# Write CSV data to target file
csv_writer.writerow([date, latitude, longitude, time_zone, offset, humidity, temperature_low, temperature_high])
#%% Close the target file
output_file.close()