f = open('data', 'r')
for line in f:
line = line.split(";;")
lines.append(line)
if len(lines) >= 10000:
# consume the lines which have been read
print lines
del lines[:]
if lines:
# consume the lines which have been read
print lines
cat data
73231701-201610;;shop_id::::73231701;;shop_name::::邂逅魅影;;platform_name::::xxxx;;shop_type::::个人卖家;;shop_loc::::xxx;;gold_seller::::否;;market_name::::taobao;;description::::NULL;;service::::NULL;;logistics::::NULL;;shop_owner::::洋洋103105;;create_time::::2012-08-15;;credit::::爱心4;;shop_age::::4;;co_name::::NULL;;shop_link::::https://shop73231701.example.com
73295319-201610;;shop_id::::73295319;;shop_name::::唯美为你最美;;platform_name::::xxxx;;shop_type::::个人卖家;;shop_loc::::xxx;;gold_seller::::否;;market_name::::taobao;;description::::NULL;;service::::NULL;;logistics::::NULL;;shop_owner::::chenyan121166563;;create_time::::2012-08-20;;credit::::钻石3;;shop_age::::4;;co_name::::NULL;;shop_link::::
上面的文件实际会很长,我这里只是写了两行,仅供参考。
“for line in f”每次都只会读取一行数据到内存,我们可以设置一个buffer,比如每10000行用list暂存下,处理完了之后再继续读取文件。
这样就实现了一段一段的读取文件内容到内存。是不是很酷!
赶紧试试吧!