简体   繁体   中英

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.

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.

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)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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