import os
import unicodecsv
from collections import OrderedDict

result_by_adm = OrderedDict()

with open('original/tw_populatedplaces_p.txt', 'r') as ppl_input:
    rows = unicodecsv.DictReader(ppl_input, dialect=unicodecsv.excel_tab)
    print "Parsing data from GNS..."
    for row in rows:
        # Only NS and N entries will be used.
        if row["NT"] != "NS" and row["NT"] != "N":
            continue
        adm_data = result_by_adm.setdefault(row["ADM1"], OrderedDict())
        entry = adm_data.setdefault(row["UFI"], OrderedDict())
        if len(entry) < 1:
            # Input basic data if not duplicated.
            entry["lat"] = row["LAT"]
            entry["lon"] = row["LONG"]
            entry["place"] = "village"
            entry["name"] = ""
            entry["name:en"] = ""
            entry["GNS:id"] = row["UFI"]
            entry["GNS:dsg_code"] = row["DSG"]
            entry["GNS:dsg_string"] = "populated place" # XXX
            entry["source"] = "GNS"
        # Fill the name
        if row["NT"] == "NS":
            entry["name"] = row["FULL_NAME_ND_RO"]
        else: # It is the N (approved)
            entry["name:en"] = row["FULL_NAME_ND_RO"]

    print "Done, writing data to CSVs..."
    fieldnames = ["lat", "lon", "place", "name", "name:en", "GNS:id", "GNS:dsg_code", "GNS:dsg_string", "source"]
    if not os.path.exists("ppl"):
        os.makedirs("ppl")
    for adm, result in result_by_adm.iteritems():
        with open('ppl/adm-%s.csv'%adm, "w") as output:
            writer = unicodecsv.DictWriter(output, fieldnames = fieldnames)
            writer.writeheader()
            for entry in result.itervalues():
                writer.writerow(entry)
    print "OK!"

@@ -1221,24 +1221,58 @@
 to CSVs...%22%0A
+    count = 0%0A    count_nonzh = 0%0A
     fieldnam
@@ -1742,16 +1742,193 @@
     
-print %22OK!%22
+            count = count + 1%0A                if not entry%5B%22name%22%5D:%0A                    count_nonzh = count_nonzh + 1%0A    print %22OK! %25d places, %25d has no Chinese name%22%25(count, count_nonzh)
 %0A

Add a total count.