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>There are a few problems:</p>

<ol>
<li>Your delimiter should be a comma instead of a semicolon if it's a CSV.</li>
<li>You are splitting on a date string instead of a delimiter when you call <code>a.split(x[i])</code>. You probably want to split on a <code>.</code>, since that's what's separating the date fields.</li>
</ol>

<p>Without changing too much code, the following code works for me. It wasn't clear from your question what you want to actually do with the data, but I tried to demonstrate how you would get it.</p>

<pre><code>import csv

with open('p2.csv') as csvfile:
    reader = csv.DictReader(
        csvfile, fieldnames=('date', 'stations', 'pcp'), delimiter=',', quotechar='|')
    next(reader)  # skip header row
    x = [row['date'] for row in reader]

for date_str in x:
    day, month, year = date_str.split('.')
    print(day, month, year)
</code></pre>

<p><strong>Output</strong>:</p>

<pre><code>1 01 1979
2 01 1979
3 01 1979
1 01 1979
2 01 1979
3 01 1979
</code></pre>

python date csv