简体   繁体   English

python 2.7 - csv 文件从 2 列中提取数据

[英]python 2.7 - csv file pull data from 2 columns

So I have a csv file and I need to find only the specific lines that match a criteria and then count how many occurrences of those lines are.所以我有一个 csv 文件,我只需要找到符合条件的特定行,然后计算这些行出现的次数。

Example file:示例文件:

col1    col2    col3

david1
david2
david3
david4

david1          sev5
david1          sev5
david1          sev4
david2          sev5
david2          sev1
david3          sev5
david3          sev2
david3          sev2
david4          sev1

The desired info I need is to only find lines that match for example:我需要的所需信息是只找到匹配的行,例如:

david1 : sev5
david1 : sev4
david3 : sev2

And then give me a total of how many it has found.然后给我一共找到了多少。

I've searched around using dictionaries and lists but for some reason I just don't understand how they could skip over certain sections of the file and then count specific lines that match a search.我已经使用字典和列表进行了搜索,但出于某种原因,我只是不明白他们如何跳过文件的某些部分,然后计算与搜索匹配的特定行。

I very much still have my 'L' plates on python and some kind help would be appreciated.我在 python 上仍然有我的“L”牌,一些帮助将不胜感激。

Thanks谢谢

try this:尝试这个:

list_of_line = []

result1 = []
result2 = []
result3 = []

file.csv = open("some.csv","r")

for l in file.csv:
    l = l.split(" ")
    for i in l:
        x = i[2]
        if x == "sev5":
            result1.append(x)
        if x == "sev4":
            result.append(x)

print len(result1)

print len(result2)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM