python 解析国家和州 并写入数据库
2013-05-21 10:44
295 查看
因为项目需要,需要用户在客户端选择国家和州。然后提交给服务器。
搜索了一下暂时没有这些。服务器用的是js的代码。说要不要返回API。
我想了下还是算了。正好项目采用了BeeFramework,提供对象关系映射。
考虑本地DB,直接转换成对象使用
拿到js地址。http://imybags.com/Js/imybags.countries.js
因为 国家的比较少。直接复制到python内部。
其他部分保存txt用python加载文件解析匹配到国家。然后写入数据库。
直接上代码
搜索了一下暂时没有这些。服务器用的是js的代码。说要不要返回API。
我想了下还是算了。正好项目采用了BeeFramework,提供对象关系映射。
考虑本地DB,直接转换成对象使用
拿到js地址。http://imybags.com/Js/imybags.countries.js
因为 国家的比较少。直接复制到python内部。
其他部分保存txt用python加载文件解析匹配到国家。然后写入数据库。
直接上代码
#/usr/bin/python #coding: utf8 import os,sys import sqlite3 __author__ = 'watsy' scountry = """ "Afghanistan","Albania","Algeria","American Samoa","Angola","Anguilla","Antartica","Antigua and Barbuda","Argentina","Armenia","Aruba","Ashmore and Cartier Island","Australia","Austria","Azerbaijan","Bahamas","Bahrain","Bangladesh","Barbados","Belarus","Belgium","Belize","Benin","Bermuda","Bhutan","Bolivia","Bosnia and Herzegovina","Botswana","Brazil","British Virgin Islands","Brunei","Bulgaria","Burkina Faso","Burma","Burundi","Cambodia","Cameroon","Canada","Cape Verde","Cayman Islands","Central African Republic","Chad","Chile","China","Christmas Island","Clipperton Island","Cocos (Keeling) Islands","Colombia","Comoros","Congo, Democratic Republic of the","Congo, Republic of the","Cook Islands","Costa Rica","Cote d'Ivoire","Croatia","Cuba","Cyprus","Czeck Republic","Denmark","Djibouti","Dominica","Dominican Republic","Ecuador","Egypt","El Salvador","Equatorial Guinea","Eritrea","Estonia","Ethiopia","Europa Island","Falkland Islands (Islas Malvinas)","Faroe Islands","Fiji","Finland","France","French Guiana","French Polynesia","French Southern and Antarctic Lands","Gabon","Gambia, The","Gaza Strip","Georgia","Germany","Ghana","Gibraltar","Glorioso Islands","Greece","Greenland","Grenada","Guadeloupe","Guam","Guatemala","Guernsey","Guinea","Guinea-Bissau","Guyana","Haiti","Heard Island and McDonald Islands","Holy See (Vatican City)","Honduras","Hong Kong","Howland Island","Hungary","Iceland","India","Indonesia","Iran","Iraq","Ireland","Ireland, Northern","Israel","Italy","Jamaica","Jan Mayen","Japan","Jarvis Island","Jersey","Johnston Atoll","Jordan","Juan de Nova Island","Kazakhstan","Kenya","Kiribati","Korea, North","Korea, South","Kuwait","Kyrgyzstan","Laos","Latvia","Lebanon","Lesotho","Liberia","Libya","Liechtenstein","Lithuania","Luxembourg","Macau","Macedonia, Former Yugoslav Republic of","Madagascar","Malawi","Malaysia","Maldives","Mali","Malta","Man, Isle of","Marshall Islands","Martinique","Mauritania","Mauritius","Mayotte","Mexico","Micronesia, Federated States of","Midway Islands","Moldova","Monaco","Mongolia","Montserrat","Morocco","Mozambique","Namibia","Nauru","Nepal","Netherlands","Netherlands Antilles","New Caledonia","New Zealand","Nicaragua","Niger","Nigeria","Niue","Norfolk Island","Northern Mariana Islands","Norway","Oman","Pakistan","Palau","Panama","Papua New Guinea","Paraguay","Peru","Philippines","Pitcaim Islands","Poland","Portugal","Puerto Rico","Qatar","Reunion","Romainia","Russia","Rwanda","Saint Helena","Saint Kitts and Nevis","Saint Lucia","Saint Pierre and Miquelon","Saint Vincent and the Grenadines","Samoa","San Marino","Sao Tome and Principe","Saudi Arabia","Scotland","Senegal","Seychelles","Sierra Leone","Singapore","Slovakia","Slovenia","Solomon Islands","Somalia","South Africa","South Georgia and South Sandwich Islands","Spain","Spratly Islands","Sri Lanka","Sudan","Suriname","Svalbard","Swaziland","Sweden","Switzerland","Syria","Taiwan","Tajikistan","Tanzania","Thailand","Tobago","Toga","Tokelau","Tonga","Trinidad","Tunisia","Turkey","Turkmenistan","Tuvalu","Uganda","Ukraine","United Arab Emirates","United Kingdom","Uruguay","USA","Uzbekistan","Vanuatu","Venezuela","Vietnam","Virgin Islands","Wales","Wallis and Futuna","West Bank","Western Sahara","Yemen","Yugoslavia","Zambia","Zimbabwe" """ #获得国家 country = scountry.strip(' ').replace("\'",'').replace('\n','').replace('"', '').split(',') country_and_states = {} with open('states.txt') as fp: sstates = fp.read() s_state_list = sstates.split(';') nindex = -1 #拆分城市 for value in s_state_list: nstart = value.find('"') value = value[nstart + 1: -1] states = value.split('|') key = country[nindex + 1] country_and_states[key] = states nindex += 1 # print country_and_states def insert(c,conn, countryname, value): sql = r"""select cID from lcCountryObject where `name` = "%s";""" % (countryname) c.execute(sql) res = c.fetchall() if res: for state in value: sql = r"""insert into lcStatesObject (`cID` , `name`) values (%s , "%s") """ % (res[0][0], state) c.execute(sql) conn.commit() return True return False conn = sqlite3.connect(r'countrys.db') c = conn.cursor() c.execute("create table if not exists lcCountryObject (cID integer primary key unique, name text DEFAULT '')") c.execute("create table if not exists lcStatesObject (sID integer primary key unique, cID integer DEFAULT '0', name text DEFAULT '')") c.execute("create index if not exists index_lcCountryObject on lcCountryObject ( cID )") c.execute("create index if not exists index_lcStatesObject on lcStatesObject ( sID )") for key in country_and_states: # print country_and_states[key] if insert(c, conn, key, country_and_states[key]): pass else: sql = r"""insert into lcCountryObject (`name`) values ( "%s" );""" % (key) c.execute(sql) conn.commit() insert(c, conn, key , country_and_states[key]) c.close() conn.close()
相关文章推荐
- Python实现读写sqlite3数据库并将统计数据写入Excel的方法示例
- Python爬虫-爬取51job.com 招聘信息并写入文件和数据库mysql
- Python用ET包解析XML文件,写入到CSV中
- 4000 python 快速写入postgresql数据库方法
- python 爬取数据,并写入数据库
- python 记一次将数据库查询结果写入到表格经历
- [Python]快速解析数据库视图XML配置获取数据库字段说明
- 通过js获取前台数据向一般处理程序传递Json数据,并解析Json数据,将前台传来的Json数据写入数据库表中
- 《项目经验》--通过js获取前台数据向一般处理程序传递Json数据,并解析Json数据,将前台传来的Json数据写入数据库表中
- python利用inotify实现把nginx日志实时写入数据库
- 【Python】beautifusoup解析HTML并将数据写入文件
- python远程连接mysql以及pandas.DataFrame.to_sql写入数据库
- HTMLParser解析网页,提取链接地址、标题名称,并插入数据库 分类: python 小练习 HTMLParser 2014-02-19 09:57 519人阅读 评论(0) 收藏
- 【Python小工具】Python实现批量解析PDF文件提取内容并写入到Excel中
- [置顶] 【python 数据库写入】python把数据框数据写入mongodb、mysql
- python读写sqlite3数据库并将统计数据写入excel
- 如何利用python+mySQL写个数据库并且利用python将所需变量值写入数据库
- 《项目经验》--通过js获取前台数据向一般处理程序传递Json数据,并解析Json数据,将前台传来的Json数据写入数据库表中
- 【详解】Python从数据库提取数据写入txt
- python 接受远程数据并写入数据库的相关模块