python如何读取百万级的csv文件-在Python中从巨大的CSV文件中读取随机线

1586010002-jmsa.png

I have this quite big CSV file (15 Gb) and I need to read about 1 million random lines from it.

As far as I can see - and implement - the CSV utility in Python only allows to iterate sequentially in the file.

It's very memory consuming to read the all file into memory to use some random choosing and it's very time consuming to go trough all the file and discard some values and choose others, so, is there anyway to choose some random line from the CSV file and read only that line?

I tried without success:

import csv

with open('linear_e_LAN2A_F_0_435keV.csv') as file:

reader = csv.reader(file)

print reader[someRandomInteger]

A sample of the CSV file:

331.093,329.735

251.188,249.994

374.468,373.782

295.643,295.159

83.9058,0

380.709,116.221

352.238,351.891

183.809,182.615

257.277,201.302

61.4598,40.7106

解决方案import random

filesize = 1500 #size of the really big file

offset = random.randrange(filesize)

f = open('really_big_file')

f.seek(offset) #go to random position

f.readline() # discard - bound to be partial line

random_line = f.readline() # bingo!

# extra to handle last/first line edge cases

if len(random_line) == 0: # we have hit the end

f.seek(0)

random_line = f.readline() # so we'll grab the first line instead

As @AndreBoos pointed out, this approach will lead to biased selection. If you know min and max length of line you can remove this bias by doing the following:

Let's assume (in this case) we have min=3 and max=15

1) Find the length (Lp) of the previous line.

Then if Lp = 3, the line is most biased against. Hence we should take it 100% of the time

If Lp = 15, the line is most biased towards. We should only take it 20% of the time as it is 5* more likely selected.

We accomplish this by randomly keeping the line X% of the time where:

X = min / Lp

If we don't keep the line, we do another random pick until our dice roll comes good. :-)

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值