-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathread_csv.py
48 lines (42 loc) · 1.39 KB
/
read_csv.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
import csv
from datetime import datetime
import re
import numpy as np
import pandas as pd
def load_meped_csv(file_name, as_df=True, remove_duplicate_cols=True):
"""
Load the meped data into a dictionary or a pandas
DataFrame, togled by the as_df kwarg.
"""
with open(file_name) as csv_file:
reader = csv.reader(csv_file)
mydict = dict(reader)
for key, val in mydict.items():
# Remove all of the non-numeric characters
if '...' in val:
raise ValueError('The metop data is not formatted correctly.')
val = val.replace('\n', '')
val = val.replace('[', '')
val = val.replace(']', '')
val = val.replace(',', '')
mydict[key] = np.array(val.split()).astype(float)
if as_df:
df = pd.DataFrame(data=mydict)
df['day'] = df['dom']
df['dateTime'] = pd.to_datetime(
df[['year', 'month', 'day', 'Hour', 'minute', 'second']]
)
df.index = df['dateTime']
if remove_duplicate_cols:
drop_cols = [
'dateTime', 'year', 'month', 'day',
'Hour', 'minute', 'second', 'dom'
]
df = df.drop(drop_cols, axis=1)
return df
else:
return mydict
if __name__ == "__main__":
file_name = './csv_daily_data/data_metop0220061206.csv'
df = load_meped_csv(file_name)
print(df)