Divide date as day,month and year
<p>l have 41 year-dataset and l would like to do some calculations by using these dataset but for this, l need to divide the date into day, month and year respectively.</p>

<p>example dataset(csv file data)</p>

<pre><code> date      stations     pcp
1.01.1979   6   1.071
2.01.1979   6   5.909
3.01.1979   6   9.134
1.01.1979   5   1.229
2.01.1979   5   0.014
3.01.1979   5   3.241
</code></pre>

<p>l need to convert these data in this:</p>

<pre><code>day  month   year   stations   pcp
 1    01    1979     6         1.071
 2    01    1979     6         5.909
 3    01    1979     6         9.134
</code></pre>

<p>when l run code , it stops and l have to close it. No error message. how can l correct this? l m a fresh user and probably ,there are many mistakes. l hope l can learn my mistake here.l made two try and l m not sure which one is correct
here is my code:</p>

<pre><code>from datetime import date, datetime, timedelta,time
import csv
import numpy as np

a=[]
dd=[]
mm=[]
yy=[]
with open('p2.csv') as csvfile:
    x=[]
    reader = csv.DictReader(csvfile,fieldnames=("date","stations","pcp"),delimiter=';', quotechar='|')
    for row in reader:
       x.append(row["date"])

#try1
for i in range(len (x)):
   day,month,year=a.split(x[i])
   d=int(day)
   m=int(month)
   y=int(year)
   dd.append(d)
   mm.append(m)
   yy.append(y)



#try2
"""for i in range(len(x)):
   an=x[i]
   y=datetime.datetime.strftime(an, '%Y')
   d=datetime.datetime.strftime(an, '%d')
   m=datetime.datetime.strftime(an, '%m')
   dd.append(d)
   mm.append(m)
   yy.append(y)"""
</code></pre>

<p>this script will get you a list of lists with your values and will also write the final list into a csv.</p>

<pre><code>import csv

with open('p2.csv') as csvfile:
    x=[]
    reader = csv.reader(csvfile,delimiter=',', quotechar='|')
    for num, row in enumerate(reader):
      for i, item in enumerate(row):
        if i ==0:
          yy = []
          d, m, y = item.split('.')
          yy.append(d)
          yy.append(m)
          yy.append(y)

        else:
          yy.append(item)
      x.append(yy)
    print x       

with open('output.csv', 'wb') as csvfile:
    datawriter = csv.writer(csvfile,delimiter=',', quotechar='|')
    for datarow in x:
      datawriter.writerow(datarow)
</code></pre>

python date csv