python快速处理文本内容_如何用python快速读取25k小txt文件内容

我在操作系统中下载了很多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,似乎效果不好

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值