1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179
| #!/usr/bin/env python
# coding: UTF-8 -*-
import sys # Gestion système
import datetime # Gestion des dates
# Fonction de vérification de date
# Fonction de vérification de date
def checkDate(
dateTxt):
# Extraction jj/mm/aaaa
jj=int(dateTxt[0:2])
mm=int(dateTxt[3:5])
aa=int(dateTxt[6:10])
if aa < 1900:
return "FALSE"
else:
# Création d'une date avec aa, mm et jj
try:
# Si la date est incorrecte, il y aura une levée d'exception
datetime.date(aa, mm, jj)
# Si exception
except ValueError:
# Cas particulier de l'année
if aa >= 1900:
# Renvoi date rectifiée
return "01/01/%s" % aa
else:
# La date entrée n'est pas valide
return "FALSE"
# if
# Ce bloc if/else peut se simplifier en
return aa >= 1900 and ("01/01/%s" % aa) or None
# try
# La date entrée est valide - On la renvoie telle quelle
return dateTxt
# checkDate()
# Ouverture fichier
try:
fp=open("PROP.140.NO679", "r")
except IOError:
print "Impossible d'ouvrir PROP.140.NO679 - Abandon"
sys.exit(1)
# try
i=0
# Traitement fichier PROP.140.NO679
for lig in fp:
i=i+1
if i != 1:
# Suppression '\n' de fin de ligne
if lig[-1] == "\n": lig=lig[0:-1]
# Extraction des infos
dept=lig[0:2]
frmjur=lig[37]
naissance=lig[334:344]
expnee=lig[402:405]
pays=lig[248:251]
gtyp3=lig[116]
gtyp=lig[116:120]
dlign3=lig[120:150]
dlign4=lig[150:186]
dlign5=lig[186:216]
dlign6=lig[216:248]
l1=""
l2=""
l3=""
cp=""
ville=""
nom_prenom=lig[56:116].split("/")
nom=nom_prenom[0].strip().replace("'", "''")
prenom=len(nom_prenom) >=2\
and nom_prenom[1].strip().replace("'", "''")\
or ""
nomusage=lig[289:319].strip().replace("'", "''")
prenomusage=lig[319:334].strip().replace("'", "''")
civilite=lig[286:289].strip()
majic3="NULL"
# Analyse des infos
if naissance != " ":
datenaissance=checkDate(naissance)
naissance=datenaissance != "FALSE"\
and ("cast('%s 00:00:00' AS DATETIME)" % datenaissance)\
or "NULL"
else:
naissance="NULL"
frmjur="'%s'" % (frmjur == " " and "P" or frmjur)
if expnee == "EXP":
dnomcp=lig[405:435].strip().replace("'", "''")
dprncp=lig[435:450].strip().replace("'", "''")
else:
dnomcp="NULL"
dprncp="NULL"
# if
if pays == " ": pays="F"
if gtyp[1:3] == "88":
if gtyp[0] == "2": l1=dlign3.strip().replace("'", "''")
l2=dlign4.strip().replace("'", "''")
l3=dlign5.strip().replace("'", "''")
else:
if gtyp3 == "2":
l1=dlign3.strip().replace("'", "''")
l2=dlign4.strip().replace("'", "''")
l3=dlign5.strip().replace("'", "''")
else:
l1=dlign4.strip().replace("'", "''")
l2=dlign5.strip().replace("'", "''")
# if
# if
cp=dlign6[0:5]
ville=dlign6[6:32].strip().replace("'", "''")
nom=nom != "" and ("%s" % nom) or "NULL"
prenom=prenom != "" and ("%s" % prenom) or "NULL"
nomusage=nomusage != "" and ("%s" % nomusage) or "NULL"
prenomusage=prenomusage != "" and ("%s" % prenomusage) or "NULL"
l1=l1 != "" and ("'%s'" % l1) or "NULL"
l2=l2 != "" and ("'%s'" % l2) or "NULL"
l3=l3 != "" and ("'%s'" % l3) or "NULL"
cp=cp != "" and ("'%s'" % cp) or "NULL"
ville=ville != "" and ("'%s'" % ville) or "NULL"
majic2=lig[0:12].strip()
indivision=lig[25:26].strip()
indivision=indivision != "" and ("'%s'" % indivision) or "NULL"
droit=lig[24:25].strip()
droit=droit != "" and ("'%s'" % droit) or "NULL"
destimpot=lig[26:27].strip()
destimpot=destimpot != "" and ("'%s'" % destimpot) or "NULL"
print "INSERT INTO [TESTCad_NORM09].[dbo].[Majicbis] ([Majic2],[Majic3],[Droit_Code],[Indivision_Code],[Destimpot],[Majic3_Code], [Civilite_Code],[Nom],[Prenom],[Nom_Usage],[Prenom_Usage],[Representant],[AdresseL1],[AdresseL2],[AdresseL3],[CodePostal],[Commune_Nom],[Pays_Code],[Adresse_Exacte],[DateNaissance],[NomEpx] ,[PrenomEpx] ,[FrmJur_Code] ,[CRPF_ID] ,[DateCreation] ,[DateModification],[AuteurCreation] ,[AuteurModification] ,[tmpClePropri]) VALUES ('%s','%s%s',%s,%s,%s,'%s%s','%s','%s','%s','%s','%s',NULL,%s,%s,%s,%s,%s,'%s',1,%s,%s,%s,%s,%s,CURRENT_TIMESTAMP,NULL,'RL',NULL,NULL);" % (
majic2,
dept,
majic3,
droit,
indivision,
destimpot,
dept,
majic3,
civilite,
nom,
prenom,
nomusage,
prenomusage,
l1,
l2,
l3,
cp,
ville,
pays,
naissance,
dnomcp,
dprncp,
frmjur,
dept,
)
# for
# Fermeture fichier
fp.close() |