f = open('./Ham_tmin.txt')

"""
Explanation: Read txt files line by line
It's nice, when we can import data from ASCII file with some nice tool like pandas, however there are many cases, when you have to parse data line by line by yourself.
Let's open some file that is already familiar:
End of explanation
"""


f

"""
Explanation: f is now just a file handler:
End of explanation
"""


lines = f.readlines()

lines

lines[0]

lines[5]

one_line = lines[10]

one_line

"""
Explanation: Read lines to list
End of explanation
"""


one_line.split(',')

one_line.split(',')[0]

one_line.split(',')[1]

"""
Explanation: We can separate this line in to several parts (that wil form another list)
End of explanation
"""


for line in lines[:10]:
    print(line)

for line in lines[:10]:
    print(line.split(','))

for line in lines[:10]:
    print(line.split(',')[0])

"""
Explanation: Exersise

Split one_split variable in a way, that 0$^{th}$ element of resulting list contains only year.

If we can split one line, we can split many in a loop. Here we use only first 10 elements from out list:
End of explanation
"""


years = []
for line in lines[:10]:
    year = line.split(',')[0]
    years.append(year)
    

years

"""
Explanation: Exersice

Print first 10 temparatures from lines devided by 10.

Ok, we can print things out, but how about storing them in some usefull variable? One of the ways to do so, is to create an empty list, and append to it:
End of explanation
"""


fout = open('out.txt', 'w') # 'w' means file will be opened for writing

for year in years:
    fout.write(year+'\n')
fout.close()

# %load out.txt
1891-01-01
1891-01-02
1891-01-03
1891-01-04
1891-01-05
1891-01-06
1891-01-07
1891-01-08
1891-01-09
1891-01-10


"""
Explanation: Exercise

create variable temp with temperature values (devided by 10)
plot variable temp

We get some values, now, let's write them down.
End of explanation
"""


fout = open('out.txt', 'w')
for n in range(10):
    fout.write(years[n]+'\n')
fout.close()

# %load out.txt
1891-01-01
1891-01-02
1891-01-03
1891-01-04
1891-01-05
1891-01-06
1891-01-07
1891-01-08
1891-01-09
1891-01-10


"""
Explanation: Another variant:
End of explanation
"""


dummy = range(100,110)
dummy

fout = open('out.txt', 'w')

for n in range(10):
    fout.write(years[n]+'\t'+str(dummy[n])+'\n')

fout.close()

# %load out.txt
1891-01-01	100
1891-01-02	101
1891-01-03	102
1891-01-04	103
1891-01-05	104
1891-01-06	105
1891-01-07	106
1891-01-08	107
1891-01-09	108
1891-01-10	109


"""
Explanation: We can combine values
End of explanation
"""


fout = open('out.txt', 'w')

for n in range(10):
    fout.write('{}\t{}\n'.format( years[n], str(dummy[n])))

fout.close()

# %load out.txt

"""
Explanation: Bonus example with .format() method
End of explanation
"""


