如何在Python CSV模块中查找特定的行 [英] How to find specific row in Python CSV module

查看:76
本文介绍了如何在Python CSV模块中查找特定的行的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I need to find the third row from column 4 to the end of the a CSV file. How would I do that? I know I can find the values from the 4th column on with row[3] but how do I get specifically the third row?

推荐答案

您可以将csv阅读器对象转换为列表列表...行存储在列表中,其中包含列列表.

You could convert the csv reader object into a list of lists... The rows are stored in a list, which contains lists of the columns.

所以:

csvr = csv.reader(file)
csvr = list(csvr)
csvr[2]     # The 3rd row
csvr[2][3]  # The 4th column on the 3rd row.
csvr[-4][-3]# The 3rd column from the right on the 4th row from the end

这篇关于如何在Python CSV模块中查找特定的行的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆