Pour lire/écrire un fichier au format dbf (Note: pour la définition de dbf voir l'article dBase), il existe le package python très complet "dbf". Pour utiliser ce package télécharger dbf puis installer celui-ci avec la commande python setup.py install. Une fois cette étape terminée, vous pouvez commencer à travailler avec des fichiers aux formats bdf. Vous pouvez, par exemple, télécharger TM_WORLD_BORDERS_SIMPL-0.3.zip sur le site thematicmapping
et essayer d'extraire les informations contenues dans ce fichier comme:
>>> import dbf>>> table = dbf.Table('TM_WORLD_BORDERS_SIMPL-0.3.dbf')>>> print tableTable: TM_WORLD_BORDERS_SIMPL-0.3.dbfType: dBase III PlusCodepage: cp1252 (Windows ANSI)Status: closedLast updated: 2014-05-28Record count: 246Field count: 11Record length: 99--Fields--0) fips C(2)1) iso2 C(2)2) iso3 C(3)3) un N(3,0)4) name C(50)5) area N(7,0)6) pop2005 N(10,0)7) region N(3,0)8) subregion N(3,0)9) lon N(8,3)10) lat N(7,3)>>>
Il est aussi possible de faire une boucle sur l'ensemble des valeurs:
>>> table.open()>>> for record in table:>>> print record.name, record.fips, record.lon, record.lat, record.region, record.area
qui donne le résultat suivant:
Antigua and Barbuda AC -61.783 17.078 19 44Algeria AG 2.632 28.163 2 238174Azerbaijan AJ 47.395 40.43 142 8260Albania AL 20.068 41.143 150 2740Armenia AM 44.563 40.534 142 2820Angola AO 17.544 -12.296 2 124670American Samoa AQ -170.73 -14.318 9 20Argentina AR -65.167 -35.377 19 273669Australia AS 136.189 -24.973 9 768230Bahrain BA 50.562 26.019 142 71Barbados BB -59.559 13.153 19 43Bermuda BD -64.709 32.336 19 5Bahamas BF -78.014 24.628 19 1001Bangladesh BG 89.941 24.218 142 13017Belize BH -88.602 17.219 19 2281Bosnia and Herzegovina BK 17.786 44.169 150 5120Bolivia BL -64.671 -16.715 19 108438...
Erreurs associées
Si vous obtenez le message d'erreur suivant en voulant utiliser le package dbf (voir aussi (How can I represent an 'Enum' in Python? )):
from enum import Enum, IntEnumImportError: No module named enum
Veuillez installer enum34 1.0
( pip install enum34 ou python setup.py install)
Recherches associées
| Liens | Site |
|---|---|
| Opening and searching dBase III (DBF) databases in Python | stackoverflow |
| Python: Fast querying in a big dbf (xbase) file | stackoverflow |
| Is there Python 3 module to read .dbf files written by ArcGIS for Desktop? | stackexchange |
| Given longitude and latitude, how to find country | stackoverflow |
| DBF reader and writer (Python recipe) | code activestate |
| Python modules for accessing .dbf (xBase) files | dbfpy package |
| thematicmapping | thematicmapping |
| Latitude+Longitude Map Downloads | dmap |
| naturalearthdata | naturalearthdata |
| What is DBF | what is |
| dBase wikipedia | wikipedia |
