我在操作系统中下载了很多html存储,现在获取了它们的内容,并提取了需要持久化到mysql的数据,
我一个接一个地使用传统的加载文件,效率不高,只需8分钟。在
欢迎提出任何建议g_fields=[
'name',
'price',
'productid',
'site',
'link',
'smallImage',
'bigImage',
'description',
'createdOn',
'modifiedOn',
'size',
'weight',
'wrap',
'material',
'packagingCount',
'stock',
'location',
'popularity',
'inStock',
'categories',
] @cost_time
def batch_xml2csv():
"批量将xml导入到一个csv文件中"
delete(g_xml2csv_file)
f=open(g_xml2csv_file,"a")
import os.path
import mmap
for file in glob.glob(g_filter):
print "读入%s"%file
ff=open(file,"r+")
size=os.path.getsize(file)
data=mmap.mmap(ff.fileno(),size)
s=pq(data.read(size))
data.close()
ff.close()
#s=pq(open(file,"r").read())
line=[]
for field in g_fields:
r=s("field[@name='%s']"%field).text()
if r is None:
line.append("\N")
else:
line.append('"%s"'%r.replace('"','\"'))
f.write(",".join(line)+"\n")
f.close()
print "done!"
我试过mmap,似乎效果不好