Getting specific line and value with Python DictReader

后端 未结 4 857
终归单人心
终归单人心 2021-01-14 08:28

I have a csv file and am trying to get one specific value on, say, line 20, column 3.

But so far, all I managed is to display all values on column 3 (here c

4条回答
  •  轮回少年
    2021-01-14 08:55

    Skipping ahead to the desired line, just as in the other answer, but with slicing on the CSV-reading iterator instead of a manual loop:

    import csv, itertools
    rownum = 20
    colname = "name"
    line = itertools.islice(d, rownum - 1, rownum).next()
    score = line[colname]
    

    (Not the most beautiful code, I know, but that's just to illustrate the point.)

提交回复
热议问题