python 3.x - How to store a row from CSV file? -


in csv file have 5 lines describing file. need store third line ignoring rest. i've managed write code ignores description headers how can store 1 line? wrote code ignore description headers

for row in range(6):        csvfile.readline() 

you can use itertools.islice 8th line 3rd line after 5 header lines i.e 8th line or index 7:

from itertools import islice  line = next(islice(csvfile, 7, 8)) # start=index 7, end= index 8 1 line  

you can use linecache.getline single line file not efficient.

if had large file itertools consume recipe might useful skip n lines:

def consume(iterator, n):     "advance iterator n-steps ahead. if n none, consume entirely."     # use functions consume iterators @ c speed.     if n none:         # feed entire iterator zero-length deque         collections.deque(iterator, maxlen=0)     else:         # advance empty slice starting @ position n         next(islice(iterator, n, n), none) 

Comments

Popular posts from this blog

c - Bitwise operation with (signed) enum value -

xslt - Unnest parent nodes by child node -

YouTubePlayerFragment cannot be cast to android.support.v4.app.Fragment -