-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathfixed_width_2.py
More file actions
executable file
·31 lines (31 loc) · 1.16 KB
/
fixed_width_2.py
File metadata and controls
executable file
·31 lines (31 loc) · 1.16 KB
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
def read_weather_data(r):
'''Read weather data from reader r in fixed-width format.
The field widths are:
4,2,2 YYYYMMDD (date)
2,2,2 DDMMSS (latitude)
2,2,2 DDMMSS (longitude)
6,6,6 FF.FFF (temp, deg. C; humidity, %; pressure, kPa)
The result is a list of tuples (not tuples of tuples),
where each tuple is of the form:
(YY, MM, DD, DD, MM, SS, DD, MM, SS, Temp, Hum, Press)'''
fields = ((4, int), (2, int), (2, int), # date
(2, int), (2, int), (2, int), # latitude
(2, int), (2, int), (2, int), # longitude
(6, float), (6, float), (6, float)) # data
result = []
# For each record
for line in r:
start = 0
record = []
# for each field in the record
for (width, target_type) in fields:
# convert the text
text = line[start:start+width]
field = target_type(text)
# add it to the record
record.append(field)
# move on
start += width
# add the completed record to the result
result.append(record)
return result