3
Well, I’m working with an open database, and it’s in CSV, I’m using DJANGO to read the CSV and so entering into the database... However is giving a flaw in the insertion of the first year,I’m trying to insert 8 years of data, the first year it inserts only 11 months, so is missing enter 1 month to properly close the data.
from django.shortcuts import render
from models import Internacao
import csv
def csvInternacao(request):
coluna = 1
mes = 1
ano = 2008
while (ano < 2017):
with open('locale', 'rb') as csvfile:
spamreader = csv.reader(csvfile, delimiter=',', quotechar='|')
for row in spamreader:
if row[0] is not None:
t = Internacao()
t.idmunicipios = row[0]
t.quantidade = row[coluna]
t.data = str(ano) + '-' + str(mes) + '-01'
t.save(force_insert=True)
print t.idmunicipios, t.data, t.quantidade
mes = mes + 1
coluna = coluna + 1
print mes
if coluna%12 == 0:
print ano
mes = 1
ano = ano + 1
Thus reading each column of csv and changing the columns automatically and changing the year, as each column is one month. Does anyone know why I’m not getting the insertion right?
Solved friend, the second way worked also... I resolved also doing
if coluna%12 == 1:
 print ano
 mes = 1
 ano = ano + 1
This print is just to debug even...but it worked 100%!! Thank you!– Marcelo Henrique Casali
@Marcelohenriquecasali if the answer really helped/solved should accept it, below the down arrow to the left of the answer
– Miguel