c# - Read chosen line from CSV -
i have big csv file 1,000,000 rows , takes 500 mb of memory. don't have read file. want read every 1 hundredth line file. seek readlines
, slow, faster readalllines
.
my code:
for (int = 0; < 10000; i++) { tableofstring[i]=file.readlines("testcsv.csv").skip(i*100).take(1).first(); //or tableofstring[i] = file.readlines("testcsv.csv").elementatordefault(i*100); }
i read readers:
a fast csv reader reading , writing csv files in c# linq csv libraryhas got solution? want read lines csv, not whole file.
readlines
not slow. problem you're re-reading file upto desired row in each iteration. (when i=1, read lines 0-100... when i=2, read lines 0-200, etc.)
you should avoid calling file.readlines
multiple times. in other words, open file 1 time , filter out lines don't want using where
. seek instead:
var filteredlines = file.readlines("testcsv.csv") .select((text, index) => new {text, index}) .where(x => x.index % 100 == 0); foreach(var line in filteredlines) { tableofstring[line.index] = line.text; }
not sure if how you're creating or using tableofstring
, if solely used these lines can straight convert linq query array (you don't have populate array in for-loop):
var tableofstring = file.readlines("testcsv.csv") .where((x, i) => % 100 == 0) .toarray();
c# performance csv readline
No comments:
Post a Comment