Getting specific line and value with Python DictReader

后端 未结 4 869
终归单人心
终归单人心 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:42

    if you only know the column number and the line number you can do like this:

    TheRow = 2      # Note that row 1 is the header row
    TheColumn = 2   # first column == 0
    for row in d:
        if d.line_num == TheRow:
            print("row %s: %s" % (str(d.line_num), str(row) ) )
            print( row[d.fieldnames[TheColumn]] )
    

    output:

    row 2: {'t1': '12', 't2': '23', 't3': '31'}
    31
    

提交回复
热议问题