-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtocsv.py
53 lines (42 loc) · 1.47 KB
/
tocsv.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
import os
import re
def get_choices():
data_choices = os.listdir('/met_office_data')
choices = []
for name in data_choices:
if name.endswith(".txt"):
choices.append(name)
return choices
def convert_all():
choices = get_choices()
for choice in choices:
convert(choice)
def convert(file):
raw_text = open(file, 'r').read()
text = 'yyyy,mm ,maxC, minC, r, r, r,'
text+= raw_text.split("hours",1)[1]
text= text.replace('mm ', 'mm')
text= text.replace(' minC', 'minC')
text=re.sub('[ \t]+', ' ', text)
text=re.sub('[ \t]+[mm][ \t]+', 'mm', text)
text=re.sub('[ \t]+[maxC][ \t]+', 'maxC', text)
text=re.sub('[ \t]+[minC][ \t]+', 'minC', text)
text=re.sub('[ \t]+[yyyy][ \t]+', 'yyyy', text)
text=re.sub('[0]{1}[\ ]', '0,', text)
text=re.sub('[1]{1}[\ ]', '1,', text)
text=re.sub('[2]{1}[\ ]', '2,', text)
text=re.sub('[3]{1}[\ ]', '3,', text)
text=re.sub('[4]{1}[\ ]', '4,', text)
text=re.sub('[5]{1}[\ ]', '5,', text)
text=re.sub('[6]{1}[\ ]', '6,', text)
text=re.sub('[7]{1}[\ ]', '7,', text)
text=re.sub('[8]{1}[\ ]', '8,', text)
text=re.sub('[9]{1}[\ ]', '9,', text)
text = text.replace('---', '0,')
text = text.replace('Provisional', '')
text = text.replace('#', ',')
text = text.replace('*', ',')
file = open("rawdata/" + file.replace('data.txt', '_raw_data.csv'), 'w')
file.write(text)
print(text)
convert_all()