Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/iphone/44.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
如何在Python CSV模块中查找特定行_Python_Csv - Fatal编程技术网

如何在Python CSV模块中查找特定行

如何在Python CSV模块中查找特定行,python,csv,Python,Csv,我需要找到从第4列到CSV文件末尾的第三行。我该怎么做?我知道我可以从第4列中找到值 第[3]行 但是,如何获得第三行呢?您可以保留一个计数器来计算行数: counter = 1 for row in reader: if counter == 3: print('Interested in third row') counter += 1 您可以将csv阅读器对象转换为列表列表。。。行存储在一个列表中,该列表包含列的列表 因此: 这是一个非常基本的代码,可以完

我需要找到从第4列到CSV文件末尾的第三行。我该怎么做?我知道我可以从第4列中找到值 第[3]行
但是,如何获得第三行呢?

您可以保留一个计数器来计算行数:

counter = 1
for row in reader:
    if counter == 3:
        print('Interested in third row')
    counter += 1

您可以将csv阅读器对象转换为列表列表。。。行存储在一个列表中,该列表包含列的列表

因此:


这是一个非常基本的代码,可以完成这项工作,您可以很容易地从中生成函数

import csv

target_row = 3
target_col = 4

with open('yourfile.csv', 'rb') as csvfile:
    reader = csv.reader(csvfile)
    n = 0
    for row in reader:
        if row == target_row:
            data = row.split()[target_col]
            break

print data

您可以使用
itertools.islice
提取所需的数据行,然后索引到其中

请注意,行和列的编号是从零开始的,而不是从一开始的

import csv
from itertools import islice

def get_row_col(csv_filename, row, col):
    with open(csv_filename, 'rb') as f:
        return next(islice(csv.reader(f), row, row+1))[col]
import csv
from itertools import islice

def get_row_col(csv_filename, row, col):
    with open(csv_filename, 'rb') as f:
        return next(islice(csv.reader(f), row, row+1))[col]