shevchenko2010 2014年02月08日
大仙帮小弟看看代码,如何将代码简化一下
with open('data_pps.xml','r') as file:
data = file.read()
conn = sqlite3.connect('SQLite_pps.db')
c = conn.cursor()
c.execute('''select keyword from mydata where level = 2''')
cc = c.fetchall() #从数据库中获取字段
list_keywords = []
for oo in cc:
list_keywords.append(oo[0].encode("utf-8"))
#return list_keywords
#pp = oo[0].encode('utf-8')
#***********************************************************************************************************************
"""筛选掉二级标签,保留数据标签"""
for i in data.split('</goods>')[:-1]:
city_name = i.split('<city_name>')[-1].split('</city_name>')[0].decode('utf-8')
site_url = i.split('<site_url>')[-1].split('</site_url>')[0].decode('utf-8')
title = i.split('<title>')[-1].split('</title>')[0].decode('utf-8')
goods_url = i.split('<goods_url>')[-1].split('</goods_url>')[0].decode('utf-8')
desc = i.split('<desc>')[-1].split('</desc>')[0].decode('utf-8')
#class = i.split('<class>')[-1].split('</class>')[0].decode('utf-8')
img_url = i.split('<img_url>')[-1].split('</img_url>')[0].decode('utf-8')
original_price = i.split('<original_price>')[-1].split('</original_price>')[0].decode('utf-8')
sale_price = i.split('<sale_price>')[-1].split('</sale_price>')[0].decode('utf-8')
sale_rate = i.split('<sale_rate>')[-1].split('</sale_rate>')[0].decode('utf-8')
sales_num = i.split('<sales_num>')[-1].split('</sales_num>')[0].decode('utf-8')
start_time = i.split('<start_time>')[-1].split('</start_time>')[0].decode('utf-8')
site_name = i.split('<site_name>')[-1].split('</site_name>')[0].decode('utf-8')
close_time = i.split('<close_time>')[-1].split('</close_time>')[0].decode('utf-8')
merchant_name = i.split('<merchant_name>')[-1].split('</merchant_name>')[0].decode('utf-8')
merchant_tel = i.split('<merchant_tel>')[-1].split('</merchant_tel>')[0].decode('utf-8')
spend_start_time = i.split('<spend_start_time>')[-1].split('</spend_start_time>')[0].decode('utf-8')
spend_close_time = i.split('<spend_close_time>')[-1].split('</spend_close_time>')[0].decode('utf-8')
merchant_addr = i.split('<merchant_addr>')[-1].split('</merchant_addr>')[0].decode('utf-8')
hot_area = i.split('<hot_area>')[-1].split('</hot_area>')[0].decode('utf-8')
longitude = i.split('<longitude>')[-1].split('</longitude>')[0].decode('utf-8')
latitude = i.split('<latitude>')[-1].split('</latitude>')[0].decode('utf-8')
c.execute('''insert into api_data(city_name,site_url,title,goods_url,desc,img_url,original_price,sale_price,sale_rate,
sales_num,start_time,close_time,merchant_name,merchant_tel,spend_start_time,spend_close_time,
merchant_addr,hot_area,longitude,latitude,site_name)
values (?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?)''',
(city_name,site_url,title,goods_url,desc,img_url,original_price,sale_price,sale_rate,
sales_num,start_time,close_time,merchant_name,merchant_tel,spend_start_time,spend_close_time,
merchant_addr,hot_area,longitude,latitude,site_name))
conn.commit()
print "Write data sucess!!"
...全文
145 点赞 收藏 4
写回复
4 条回复

还没有回复,快来抢沙发~

发动态
发帖子
脚本语言(Perl/Python)
创建于2007-08-27

1.5w+

社区成员

3.3w+

社区内容

其他开发语言 脚本语言(Perl ,Python)
社区公告
暂无公告