I have strings like, $a = 'helloMister'; $b =…
August 7, 2019
HomePhp / Pear / Mysql / Node.jsPHP数组,Python:世界各国简称,国家语言,人口,位置, List of all countries in different formats (ISO, tld, capital, language, population)
PHP数组,Python:世界各国简称,国家语言,人口,位置, List of all countries in different formats (ISO, tld, capital, language, population)

Python: generator.py
import codecs import urllib import requests import csv import json mapping = [ 'alpha_2', 'alpha_3', 'numeric', 'fips', 'name', 'capital', 'area', 'population', 'continent', 'tld', 'currency_code', 'currency_name', 'phone', 'postal_code_format', 'postal_code_regex', 'languages', 'geoname_id', 'neighbours', 'eqivalent_fips_code', ] countries = [] url = "http://sources.ikeepstudying.com/list-of-countries/countryInfo.txt" stream = urllib.request.urlopen(url) reader = codecs.getreader("utf-8") reader = csv.reader(reader(stream), delimiter='\t') non_comment_rows = [row for row in reader if row[0][0] != '#'] for row in non_comment_rows: if row[0][0] != '#': country = {} # read files from csv and map them to dict for i in range(0, len(row)): csv_row = row[i] csv_row_title = mapping[i] country[csv_row_title] = csv_row # add missing keys as None for key in mapping: if key not in country: country[key] = None countries.append(country) # csv keys = countries[0].keys() with open('csv/countries.csv', 'w') as file: dict_writer = csv.DictWriter(file, sorted(keys), delimiter=";") dict_writer.writeheader() dict_writer.writerows(countries) # json json_string = json.dumps(countries, sort_keys=True, ensure_ascii=False) with open('json/countries.json', 'w') as file: file.write(json_string) # readable json json_string = json.dumps(countries, ensure_ascii=False, sort_keys=True, indent=4, separators=(',', ': ')) with open('json/countries-readable.json', 'w') as file: file.write(json_string) # php (I'm sorry) php_string = '<?php\n$countries = [\n' for country in countries: php_string += ' [\n' for key in sorted(country): value = country[key] if value is not None and value != "": php_string += ' "' + key + '" => "' + value + '",\n' else: php_string += ' "' + key + '" => null,\n' php_string += ' ],\n' php_string += '\n];' with open('php/array.php', 'w') as file: file.write(php_string)
PHP: list-of-countries.php
JSON: list-of-countries.json
PHP: list-of-countries.php
Related Posts
- PHP: 通过字母大小写分割字符,大写字符分割到数组, php explode at capital letters?
-
Ubuntu下转 win7 iso 到USB Can I use Ubuntu to write a Windows 7 ISO to USB?
Method 1 Using gparted format the drive FAT16, set…
- PHP 国家时区 PHP List of timezones (Not sorted by country)
代码: <?php $timezones[] = array("abbr"=>"ACDT","name"=>"Australian Central Daylight Time","offset"=>"+10:30"); $timezones[]…
Related Posts

Magento 创建唯一优惠券 Create unique coupon code in Magento

PHP: UPS API, UPS 运费计算, UPS接口
