python read lines of an entire file, and efficiently storing the ones I want in lists -


i have text file repeating following block structure this:

el_text
layer 6
datatype 0
xy 2677000: 2316500
2677000: 2340500
2707000: 2340500
2707000: 2316500
2677000: 2316500
endel

...

and blocks repeating themselves, different values, along text file. end in endel
read , find lines example "layer 6" (or "layer 6 \n") , store these lines list. including xy coordinates of these blocks list of xy tuples [(2677000, 2316500), ...]
final list
onelayer6polygon = [(2677000, 2316500), (2677000, 2340500), ...]
listofpolygonsl6 = [[(2677000, 2316500), (2677000, 2340500), ...], [...], ...]
how can efficiently in python? i.e. reading lines of file, , once, advancing position lseek skip coordinates not layer want.
far understood for line in file: read lines until eof, need read , store (and later process) ones layer number specify. doing while loop , handling index not detecting eof, right?

as far understood for line in file: read lines until eof, need read , store (and later process) ones layer number specify.

yes , no. looping on file read file in entirety line-by-line, until reach end of file. read 1 line after another, without storing contents anyway. it’s skip lines not interested in, , store information want keep encounter them.

note have read file in order iterate on lines. there no concept of jumping between lines, can seek on byte (or character) level, in order know line need check characters looking line breaks. unless know every block x characters in total, have read blocks not interested in in order find start of next block.

that being said, solve sort of task state machine: read file line line, , read line, may choose change state of machine set different “mode”.

in case, mode may “within layer 6 block”, that’s should start with:

inlayer6block = false line in file:     # strip trailing whitespace, since every line ends line break     line = line.rstrip()      # if see `layer 6` line, start our block     if line == 'layer 6':         inlayer6block = true      # if see `endel` line, no longer in block     elif line == 'endel':         inlayer6block = false 

so that’s left add logic handle case in between, when inlayer6block true. i’ll leave expand on code above. in general, want have list stores content within block, append long in inlayer6block state. in order store every block separately, have block append single-block list when block ends.


Comments

Popular posts from this blog

javascript - jQuery: Add class depending on URL in the best way -

caching - How to check if a url path exists in the service worker cache -

Redirect to a HTTPS version using .htaccess -