Reputation: 33
I have a text file with weather data from over 60 years that looks like this: (if its messed up just copy into text file!)
Stnr Dato DD06 DD12 DD18 FFM FXM POM TAM UUM
50540 07.01.1957 150 170 170 6.2 8.8 1010.6 6.3 94
50540 08.01.1957 160 160 200 7.2 9.8 1001.8 8.0 99
50540 09.01.1957 290 200 160 8.1 13.3 990.2 5.7 91
I want to write a function that reads data from this file for one year, where the year is determined by the user, how do I do this? And then write the avg value for FFM and TAM for each month in a new file.
Upvotes: 0
Views: 742
Reputation: 6070
This looks like a .csv
file or more specifically a tab delimited file
.
I would recommend reading the file into some python data format, list or dictionary perhaps, and then doing a search across the date column.
The process is:
I answered these steps with a dictionary, the method data_to_python
below, and the method search_by_year
, respectively.
Here is my rough attempt, test output below...
def data_to_python(data_file_name):
with open(data_file_name,'r') as f:
data = []
first = True
for line in f:
if first:
first = False
datanames = line.split('\t')
else:
temp = {}
for i,item in enumerate(line.split('\t')):
temp[datanames[i]] = item
data.append(temp)
return data
def searchByYear(data,year):
temp = []
for entry in data:
if entry['Dato'].endswith(str(year)):
temp.append(entry)
return temp
I put the input you provided into a txt file called test.txt
Sample output:
>>> data = dataToPython('test.txt')
>>> searchByYear(data,1957)
[{'FFM': '6.2', 'DD18': '170', 'DD06': '150', 'Stnr': '50540', 'DD12': '170', 'FXM':'8.8', 'Dato': '07.01.1957', 'POM': '1010.6', 'UUM\n': '94\n', 'TAM': '6.3'}, {'FFM': '7.2', 'DD18': '200', 'DD06': '160', 'Stnr': '50540', 'DD12': '160', 'FXM': '9.8', 'Dato': '08.01.1957', 'POM': '1001.8', 'UUM\n': '99\n', 'TAM': '8.0'}, {'FFM': '8.1', 'DD18': '160', 'DD06': '290', 'Stnr': '50540', 'DD12': '200', 'FXM': '13.3', 'Dato': '09.01.1957', 'POM': '990.2', 'UUM\n': '91', 'TAM': '5.7'}]
>>> searchByYear(data,1956)
[]
Upvotes: 2
Reputation: 4425
I would suggest treating this as a csv file using csv.DictReader() You would specify delimiter='\t' (if the text file shown has tabs as delimiter and not space). You would loop through the rows, determine the appropriate dates using (datetime.datetime) strptime() with a format of '%m.%d.%Y'
The input row gets the dictionary keys from the first line and you accumulate the data and use numpy to get the mean for each set of data. Your output file could use cvs.DictWriter() with the keys month, FFM, TAM for the averages. You can use matplotlib.pyplot to create a map of the values by month to see how they chanege. A bar graph of two different colors , for example.
Upvotes: 1