简体   繁体   English

使用python在csv文件中以特定方式添加列值

[英]to add column values in a specific manner in a csv file using python

i have a csv file similar to the following : 我有一个类似于以下内容的csv文件:

title  title2  h1  h2  h3 ... 
l1.1     l1     1   1   0  
l1.2     l1     0   1   0
l1.3     l1     1   0   1
l2.1     l2     0   0   1
l2.2     l2     1   0   1
l3.1     l3     0   1   1
l3.2     l3     1   1   0
l3.3     l3     1   1   0
l3.4     l3     1   1   0    

i want to be able to add the columns in the following manner: 我希望能够以以下方式添加列:
h1 ( l1.1 + l1.2+ l1.3 ) = 2 h1(l1.1 + l1.2 + l1.3)= 2
h1 ( l2.1 + l2.2 ) = 1 h1(l2.1 + l2.2)= 1
h1 ( l3.1 + l3.2 + l3.3 +l3.4) = 3 and so on for every column And i want the final count for every such value as a summarised table : h1(l3.1 + l3.2 + l3.3 + l3.4)= 3,以此类推,对于每一列,我希望每一个这样的值的最终计数作为汇总表:

title2  h1  h2  h3...
l1     2   2   1
l2     1   0   2
l3     3   4   1

how do i implement this? 我该如何实现?

Something like this should work. 这样的事情应该有效。 It takes an input in the form 它采用以下形式的输入

title,title2,h1,h2,h3
l1.1,l1,1,1,0
l1.2,l1,0,1,0
l1.3,l1,1,0,1
l2.1,l2,0,0,1
l2.2,l2,1,0,1
l3.1,l3,0,1,1
l3.2,l3,1,1,0
l3.3,l3,1,1,0
l3.4,l3,1,1,0

and outputs 和输出

title2,h1,h2,h3
l1,2,2,1
l2,1,0,2
l3,3,4,1

Tested with Python 3.1.2. 经过Python 3.1.2测试。 In Python 2.x you'll need to change the open() calls to use binary mode, and drop the newline="" bit). 在Python 2.x中,您需要将open()调用更改为使用二进制模式,并删除newline=""位)。 You can also drop the call to list() since in Python 2.x, map() already returns a list. 您还可以删除对list()的调用,因为在Python 2.x中, map()已返回列表。

import csv
import operator

reader = csv.reader(open("test.csv", newline=""), dialect="excel")
result = {}

for pos, entry in enumerate(reader):
    if pos == 0:
        headers = entry
    else:
        if entry[1] in result:
            result[entry[1]] = list(map(operator.add, result[entry[1]], [int(i) for i in entry[2:]]))
        else:
            result[entry[1]] = [int(i) for i in entry[2:]]

writer = csv.writer(open("output.txt", "w", newline=""), dialect="excel")
writer.writerow(headers[1:])

keys = sorted(result.keys())
for key in keys:
    output = [key]
    output.extend(result[key])
    writer.writerow(output)

Have a look at the csv module. 看看csv模块。 What you want to do is open the file with a csv.reader. 您要做的是使用csv.reader打开文件。 Then you iterate over the file, one row at the time. 然后,您遍历文件,一次一行。 You accumulate the results of the additions into a temporary list. 您将添加结果累积到临时列表中。 When you are done, you write this list to a new csv.writer. 完成后,将此列表写入新的csv.writer。

You might need to define a dialect as you are not really using CSV but some tab-delimited format. 您可能需要定义一种方言,因为您并不是真正使用CSV,而是使用一些制表符分隔的格式。

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

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