在js中对文本文件进行采样



我想对从api提取的文本数据输出进行采样,因为数据很大,而且每次的大小都是动态的。

let response=wait fetch(urll,options(

let testdata=wait response.text((

let str=testdata.split('\n'(

这里的测试数据量很大,所以我想进行采样,然后使用样本数据进行进一步的测试。请帮忙!!现在str存储了10000000条线,我无法从10000000个数据点中绘制出一张图,所以我需要在这里进行采样

这就是我发现的这种方法是很好的

cat yourfile.txt | perl -ne 'print if (rand() < 0.000001)'

从这里阅读,您可以使用awk命令选择"from line"->'至线路

awk 'NR >= 57890000 && NR <= 57890010' /path/to/file

awk 'NR < 57890000 { next } { print } NR == 57890010 { exit }' /path/to/file

通过这种方式,您可以在fetch url中添加参数来检索您想要的内容,然后您必须将文本拆分到临时数组中,以添加到带有偏移索引的大数组中,偏移索引是请求的"from line"编号。

bigArray = [];
const from = 200000;
const to = 250000;
let response = await fetch('urll?from='+from+'&to='+to, options);
let testdata = await response.text();  
let lines = testdata.split('n');
let idx = 0;
lines.forEach(line => {
bigArray[from + idx] = line;
idx++;
}
......

但是不管你的行大小,这可能会占用大量内存,所以也许你可以删除一些索引,例如在"从"-100000及之后";到"+"行100000,并在移动图形渲染值时重新加载数据。

最新更新