如何以表格格式写入字典数据

1 投票
1 回答
7319 浏览
提问于 2025-04-16 13:19

如何将下面的数据写成下面表格的输出格式?

这是一个字典的列表:

data=[{'date': datetime.date(2011, 2, 8), 'user': u'xxx', 'status': u'P'}, {'date': datetime.date(2011, 2, 8), 'user': u'yyy', 'status': u'P'}, {'date': datetime.date(2011, 2, 8), 'user': u'zzz', 'status': u'P'}, {'date': datetime.date(2011, 2, 9), 'user': u'xxx, 'status': u'P'}, {'date': datetime.date(2011, 2, 9), 'user': u'yyy', 'status': u'E'}, {'date': datetime.date(2011, 2, 9), 'user': u'zzz', 'status': u'E'}, {'date': datetime.date(2011, 2, 10), 'user': u'xxx', 'status': u'P'}, {'date': datetime.date(2011, 2, 10), 'user': u'yyy', 'status': u'P'}, {'date': datetime.date(2011, 2, 10), 'user': u'zzz', 'status': u'P'}]

输出格式应该是:

S.no  user  2011-02-08 2011-02-09 2011-02-10  p-total E-total total 
 1    xxx      p          p         p           3       0       3
 2    yyy      p          E         p           2       1       3
 3    zzz      p          E         E           1       2       3

请帮忙!

1 个回答

18

我对写这样的回答有点犹豫——感觉只是给出一个完整的解决方案,没什么教学价值,但我尽量让它尽可能有帮助...

如果我理解得没错,你想把你的 data 转换成 CSV 格式,每个用户占一行。你有一系列的日期,每个日期对应一列,这一列显示用户在那天的状态。然后还有一些列用来统计每种状态在所有日期中的总数,等等。你提到的输出看起来最像是用制表符分隔的 CSV,虽然正如 eumiro 指出的,它并不完全是。不过,假设你想写的是用制表符分隔的数据。你的问题中没有说明如果在 data 中发现一个用户在同一天有两种不同的状态该怎么办,所以我们先检查一下这个情况,并抛出一个异常。

注意,最后一段中的所有内容其实应该在你的问题中提到,并附上你目前为止的最佳尝试代码。

所以,使用 csv 模块中的 DictWriter 是个不错的主意,但要使用这个类,你需要为每一行准备一个字典,把列名映射到对应的值。你可以遍历 data 中的所有内容,生成一个字典的字典,把用户映射到代表该用户行的字典。你可以用类似下面的方式来实现:

from collections import defaultdict
import csv
from datetime import date

user_to_row = defaultdict(dict)

for d in data:
    user = d['user']
    status = d['status']
    row_dict = user_to_row[user]
    row_dict['user'] = user
    date_string = str(d['date'])
    if date_string in d and row_dict[date_string] != status:
        raise Exception, "Contradiction: '%s' on '%s'" % (user,date_string)
    row_dict[date_string] = status
    # If a value isn't set in one of the total columns yet, set it to 0:
    row_dict.setdefault('p-total',0)
    row_dict.setdefault('E-total',0)
    row_dict.setdefault('total',0)
    # Make sure you increment the right column:
    count_column = 'p-total' if (status == 'P') else 'E-total'
    row_dict[count_column] += 1
    # And increment the overall total column in any case:
    row_dict['total'] += 1

你应该检查一下你是否理解了里面发生的事情——尝试打印 user_to_row,看看你理解的内容是否正确。

现在你只需要遍历 user_to_row 字典中的值,并用 DictWriter 输出它们。这里需要注意的是,你不能确定每个日期都有对应的条目,所以在这种情况下,我在缺少值时插入了 Unknown

with open("hello.csv","w") as f:

    # Create the headings:
    headings = ['S.no']
    headings += [str(date(2011,2,i)) for i in xrange(6,11)]
    headings += ['user', 'date_format','p-total','E-total','total']

    writer = csv.DictWriter(f, headings, delimiter="\t")

    # The writeheader method only appeared in Python 2.7, so write the
    # headings from a dictionary that maps each heading to itself:
    writer.writerow(dict(zip(headings,headings)))

    # Assume that S.no is just a row number...
    sno = 1
    for d in user_to_row.values():
        d['S.no'] = sno
        # Fill in any unknown values with 'Unknown':
        for h in headings:
            d.setdefault(h,'Unknown')
        writer.writerow(d)
        sno += 1

关于 csv 模块 的文档应该能给你提供理解这一部分所需的额外信息。

最终的输出看起来像这样:

S.no    2011-02-06  2011-02-07  2011-02-08  2011-02-09  2011-02-10  user    date_format p-total E-total total
1   Unknown Unknown P   P   P   xxx Unknown 3   0   3
2   Unknown Unknown P   E   P   yyy Unknown 2   1   3
3   Unknown Unknown P   E   P   zzz Unknown 2   1   3

... 这里看起来有点奇怪,因为是用制表符分隔的,但在电子表格中加载时会正确显示。

撰写回答