-
Notifications
You must be signed in to change notification settings - Fork 38
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge branch 'master' of github.com:ncasuk/ncas-isc
- Loading branch information
Showing
3 changed files
with
56 additions
and
0 deletions.
There are no files selected for viewing
Binary file not shown.
Binary file not shown.
56 changes: 56 additions & 0 deletions
56
python/exercises/example_code/weather-reader-example-script.py
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,56 @@ | ||
import sys | ||
|
||
|
||
# Functions | ||
def split_and_strip(line): | ||
values = [] | ||
|
||
for value in line.split(","): | ||
values.append(value.strip()) | ||
|
||
return values | ||
|
||
|
||
def read_data_file(fpath): | ||
""" | ||
Reads weather data from CSV file. | ||
Params: | ||
- fpath: file path to CSV file | ||
Returns: | ||
- data dictionary. | ||
""" | ||
# Main code | ||
data = {} | ||
|
||
with open(fpath, 'r') as reader: | ||
|
||
# Read header | ||
header = reader.readline() | ||
col_names = split_and_strip(header) | ||
|
||
# Set up dictionary for loading | ||
for col in col_names: | ||
data[col] = [] | ||
|
||
# Read data | ||
for line in reader: | ||
data_items = split_and_strip(line) | ||
print(f"[INFO] Data items: {data_items}") | ||
|
||
for (index, item) in enumerate(data_items): | ||
col = col_names[index] | ||
value = item | ||
data[col].append(value) | ||
|
||
return data | ||
|
||
|
||
if __name__ == "__main__": | ||
# Call the function | ||
args = sys.argv[1:] | ||
|
||
for arg in args: | ||
|
||
print("[INFO] Reading from: {}".format(arg)) | ||
data = read_data_file(arg) | ||
print(data) |