python csv list比较和添加

时间:2016-01-19 22:16:53

标签: python parsing csv addition

解析&添加客户数据

Data:
('Client 1', '13.2')
('Client 1', '22.4')
('Client 1', '1.2')
('Client 2', '3.4')
('Client 3', '12.3')
('Client 3', '3.221')
('Client 4', '234.44')

尝试编写正确的循环并添加功能以获得正确的输出。

目标结果:

客户1:36.8
客户2:3.4
客户3:15.521
客户4:234.44

这是我最终正确列出数据的代码。我从哪里可以获得结果。我尝试了许多不同的循环但没有成功。

import csv

with open('clientdata.csv') as csvfile:
    reader = csv.DictReader(csvfile)
    numbers = []
    for row in reader:
       print(row['Client Name'], row['Earnings'])

5 个答案:

答案 0 :(得分:2)

你可以通过使用格式字符串(%。2f将有2个小数位)和字典跟踪谁做出多少来解决它。

clients = {}
with open('clientdata.csv') as csvfile:
    reader = csv.DictReader(csvfile)
    numbers = []
    for row in reader:
       name = row['Client Name']
       earnings = float(row['Earnings'])
       if name in clients:
           clients[name] += earnings
       else:
           clients[name] = earnings
    for client in sorted(clients):
          print("%s:%.2f" % (client, clients[client]))

答案 1 :(得分:2)

您应该创建一个包含所有客户端的字典,然后检查客户端是否已在目录中。如果客户端没有,那么添加他们的收入。如果他们在其中,那么只需添加他们现有的收入即可获得下一次收入。

以下是我将如何进行此操作的代码。希望这会有所帮助:

clientDirectory = {}
for row in reader:
    if row['Client Name'] not in clientDirectory:
        clientDirectory[row['Client Name']] = float(row['Earnings'])
    else:
        [row['Client Name']] += float(row['Earnings'])

for key in clientDirectory.keys():
    print("%s %f" %(key, clientDirectory[key]))

答案 2 :(得分:1)

您可以使用词典添加值

with open('clientdata.csv') as csvfile:
    reader = csv.DictReader(csvfile)
    results = {}

    # adding

    for row in reader:

       # default value for new client
       if row['Client Name'] not in results:
          results[row['Client Name']] = 0

       # add value
       results[row['Client Name']] += float(row['Earnings'])

    # showing

    for name, value in results.items():
        print("%s: %s" % (name,value))

但它可能是一个问题 - 字典不必保持秩序,你可以看到不同顺序的结果。

答案 3 :(得分:0)

假设你有一个这样的数据文件:

$ cat /tmp/data.csv
'Client 1', '13.2'
'Client 1', '22.4'
'Client 1', '1.2'
'Client 2', '3.4'
'Client 3', '12.3'
'Client 3', '3.221'
'Client 4', '234.44'

您可以使用defaultdict在文件中添加浮点值:

from collections import defaultdict
import csv

dd=defaultdict(float)

with open('/tmp/data.csv') as data:
    for row in csv.reader(data, quotechar="'", skipinitialspace=True):
        dd[row[0]]+=float(row[1])

print '\n'.join(["{}: {}".format(k, dd[k]) for k in sorted(dd)])      

打印:

Client 1: 36.8
Client 2: 3.4
Client 3: 15.521
Client 4: 234.44

(我使用的排序仅适用于客户端1 - 客户端9;之后您需要natural sort方法...)

答案 4 :(得分:0)

$ cat test.csv
Client 1, 13.2
Client 1, 22.4
Client 1, 1.2
Client 2, 3.4
Client 3, 12.3
Client 3, 3.221
Client 4, 234.44

然后在Python中:

from odo import odo
from pandas import DataFrame

clientDF = odo('test.csv', DataFrame)
print clientDF.groupby(['0']).sum()