Mass content adding/Software/Extracting data from NGA country file

From Meta, a Wikimedia project coordination wiki
Jump to navigation Jump to search

Note that program is under GNU GPL, not under GNU FDL. If you contribute to this program, please, add your name inside of copyright notice.

This program operates with files which can be found at http://earth-info.nga.mil/gns/html/cntry_files.html. Download xx.zip file, unzip it and use this program to dump Python dictionary in the file.

#!/usr/bin/python
#
# Copyright (C) 2006 Milos Rancic
# 
# This program is free software; you can redistribute it and/or
# modify it under the terms of the GNU General Public License
# as published by the Free Software Foundation; either version 2
# of the License, or (at your option) any later version.
# 
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
# 
# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software
# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
# 
# Text of GNU GPL license can be found at http://www.gnu.org/licenses/gnu.html

# input file: xx.txt
# ouput file: xx.pickle

import sys
import pickle

def make_list(r):
	l = {}
	first = r[0].split("\t")
	for n in range(1,len(r)):
		l[n] = {}
		row = r[n].split("\t")
		for m in range(0,len(row)):
			l[n][first[m]] = row[m]
	return l

def dump_list(l,cc):
	pickle_fn = cc + '.pickle'
	fn = file(pickle_fn,'w')
	pickle.dump(l,fn)

def main_program(file_name):
	countrycode = file_name.split('.')[0]
	rows = file(file_name).read().split("\r\n")
	list = make_list(rows)
	dump_list(list,countrycode)

try:
	# if we are calling this script from a command line
	file_name = sys.argv[1]
	main_program(file_name)

except IndexError:
	# if we are calling this script as a module
	pass