Python - 读取和比较行和列

时间:2014-02-03 06:13:52

标签: python linux python-2.7 csv

0,1,foo
0,0,foo
0,1,foo
1,1,foobar
1,1,foobar
0,1,test
1,1,foobarbar

约10,000个条目。

让它成为csv文件。

我想知道Foo第一栏有多少'0'。并且第二列中的'1'和'0'的数量分别与foo有关。

我是否读过该文件中的上一行并检查?有没有办法使用List理解来使用它?我如何在那里维持一个柜台?

预期产出:

Foo
Coloumn1 : 
No. of 0's = 3
no. of 1's = 0
column 2:
no. of 1's =2
no. of 0's =1

4 个答案:

答案 0 :(得分:1)

from collections import defaultdict, Counter
import csv

with open('myfile.csv', 'rb') as inf:
    incsv = csv.reader(inf)
    col1, col2 = defaultdict(Counter), defaultdict(Counter)
    for c1,c2,label in incsv:
        col1[label][c1] += 1
        col2[label][c2] += 1

labels = sorted(col1)
for lbl in labels:
    print('{}:'.format(lbl))
    print('Column1:')
    for entry in ['0', '1']:
        print("No. of {}'s = {}".format(entry, col1[lbl][entry]))
    print('Column2:')
    for entry in ['0', '1']:
        print("No. of {}'s = {}".format(entry, col2[lbl][entry]))

返回

foo:
Column1:
No. of 0's = 3
No. of 1's = 0
Column2:
No. of 0's = 1
No. of 1's = 2
foobar:
Column1:
No. of 0's = 0
No. of 1's = 2
Column2:
No. of 0's = 0
No. of 1's = 2
foobarbar:
Column1:
No. of 0's = 0
No. of 1's = 1
Column2:
No. of 0's = 0
No. of 1's = 1
test:
Column1:
No. of 0's = 1
No. of 1's = 0
Column2:
No. of 0's = 0
No. of 1's = 1

答案 1 :(得分:1)

构建数据

datastring = """0,1,foo
0,0,foo
0,1,foo
1,1,foobar
1,1,foobar
0,1,test
1,1,foobarbar"""

def count_data(datastring):
    datadict = {}
    for line in datastring.split('\n'):
        col1, col2, col3 = line.split(',')
        for i, colval in enumerate((col1, col2)): # doing it this way in case there are more cols
            datadict.setdefault(col3, {}).setdefault(colval, [0, 0])[i] += 1
    return datadict
datadict = count_data(datastring)

输出:

{'test': {'1': [0, 1], '0': [1, 0]}, 'foobar': {'1': [2, 2]}, 'foo': {'1': [0, 2], '0': [3, 1]}, 'foobarbar': {'1': [1, 1]}}

显示数据

def print_data(datadict):
    for key in datadict:
        print key
        for i, col in enumerate(datadict[key]):
            print 'Column', i+1, ':'
            colvalues = datadict[key][col]
            for value in (0, 1):
                print "Number of {0}'s:".format(value), colvalues[value]

输出

test
Column 1 :
Number of 0's: 0
Number of 1's: 1
Column 2 :
Number of 0's: 1
Number of 1's: 0
foobar
Column 1 :
Number of 0's: 2
Number of 1's: 2
foo
Column 1 :
Number of 0's: 0
Number of 1's: 2
Column 2 :
Number of 0's: 3
Number of 1's: 1
foobarbar
Column 1 :
Number of 0's: 1
Number of 1's: 1

答案 2 :(得分:0)

以下代码的列表推导创建了一个列表,其中包含文件中最后一列的字符串为'foo' and的每一行,当前column的字符串是您的数字寻找。打印该列表的长度将为您提供出现次数:

<强> file.txt的:

0,1,foo
0,0,foo
0,1,foo
1,1,foobar
1,1,foobar
0,1,test
1,1,foobarbar

<强>代码:

search_string = 'foo\n'
with open('file.txt', 'r') as f:
    lines = list(f)
    for column in [0, 1]: # Let's count columns from 0
        print "Column %d: " % (column)
        for number in ['0', '1']: # Strings for .csv file
            print "Number of %s's = " % (number),
            print len([line for line in lines if
                       (line.split(',')[-1] == search_string and
                       line.split(',')[column] == number)])

<强>输出:

Column 0: 
Number of 0's =  3
Number of 1's =  0
Column 1: 
Number of 0's =  1
Number of 1's =  2

答案 3 :(得分:0)

file = "a.csv"
search = "foo"
lines = open(file).readlines()
(firstcol_zero, firstcol_one, secondcol_zero, secondcol_one) = (0, 0 ,0 ,0 )
for line in lines:
    line = line.strip()
    if not line : continue
    split = line.split(',')
    if not split[2] == search: continue
    if (int(split[0]) == 0):    firstcol_zero += 1
    elif (int (split[0]) == 1): firstcol_one += 1
    if (int(split[1]) == 0):    secondcol_zero += 1
    elif (int (split[1]) == 1): secondcol_one += 1

print firstcol_zero
print firstcol_one
print secondcol_zero
print secondcol_one