Created
December 9, 2012 10:52
-
-
Save dpwiz/4244207 to your computer and use it in GitHub Desktop.
Импорт DBF ФИАС в PostgreSQL
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
{ | |
"actstat": { | |
"convert": { | |
"actstatid": "int" | |
} | |
}, | |
"addrobj": { | |
"null": [ | |
"actstat", | |
"nextid", | |
"previd", | |
"terrifnsfl", | |
"terrifnsul", | |
"ifnsfl", | |
"ifnsul", | |
"normdoc", | |
"parentguid", | |
"okato", | |
"oktmo", | |
"enddate", | |
"startdate", | |
"postalcode" | |
], | |
"convert": { | |
"aoguid": "uuid", | |
"aoid": "uuid", | |
"parentguid": "uuid", | |
"nextid": "uuid", | |
"previd": "uuid", | |
"normdoc": "uuid", | |
"actstatus": "int", | |
"aolevel": "int", | |
"centstatus": "int", | |
"currstatus": "int", | |
"operstatus": "int", | |
"livestatus": "int" | |
} | |
}, | |
"centerst": { | |
"convert": { | |
"centerstid": "int" | |
} | |
}, | |
"curentst": { | |
"convert": { | |
"curentstid": "int" | |
} | |
}, | |
"eststat": { | |
"convert": { | |
"eststatid": "int" | |
} | |
}, | |
"hststat": { | |
"convert": { | |
"hststatid": "int" | |
} | |
}, | |
"intvstat": { | |
"convert": { | |
"intvstatid": "int" | |
} | |
}, | |
"ndoctype": { | |
"convert": { | |
"ndtypeid": "int" | |
} | |
}, | |
"operstat": { | |
"convert": { | |
"operstatid": "int" | |
} | |
}, | |
"socrbase": { | |
"convert": { | |
"level": "int" | |
} | |
}, | |
"strstat": { | |
"convert": { | |
"strstatid": "int" | |
} | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env python | |
convert_sql = """ | |
BEGIN; | |
alter table {table} rename column {column} to {column}_x; | |
alter table {table} add column {column} {cast}; | |
update {table} set {column} = {column}_x; | |
alter table {table} drop column {column}_x; | |
COMMIT; | |
""" | |
def convert(table, column, cast): | |
return convert_sql.format(table=table, column=column, cast=cast) | |
def null(table, column): | |
return "UPDATE {table} SET {column} = NULL WHERE {column} = '';".format(table=table, column=column) | |
if __name__=='__main__': | |
import sys, json | |
spec = json.load(open(sys.argv[1])) | |
for table, task in spec.iteritems(): | |
for column in task.get('null', []): | |
print null(table, column) | |
for column, cast in sorted(task.get('convert', {}).iteritems()): | |
print convert(table, column, cast) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
mkdir house normdoc | |
mv house*.dbf house/ | |
mv normdoc*.dbf normdoc/ | |
for file in *.dbf; do | |
echo "Importing $file..." | |
pgdbf $file | iconv -f cp866 -t utf-8 | psql fias | |
done |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment