Python CSV在读入字典后搜索特定值

发布于 2024-10-16 08:05:40 字数 391 浏览 1 评论 0原文

我需要一些帮助,使用 Python 将特定值读取到字典中。我有一个包含用户编号的 csv 文件。所以用户 1,2,3...每个用户都在特定的部门 1,2,3...并且每个部门都在特定的建筑物 1,2,3...所以我需要知道如何列出1 号楼的部门 1 的所有用户,然后是 1 号楼的部门 2 的所有用户,依此类推。我一直在尝试并使用 csv.ReadDict 将所有内容读入大型字典中,但是如果我可以搜索我读入每个字典字典中的条目,那么这将起作用。关于如何排序这个文件有什么想法吗? CSV 包含超过 150,000 个用户条目。每行是一个新用户,列出 3 个属性,user_name、departmentnumber、department Building。有100个部门、100栋大楼和15万用户。有什么想法可以用一个简短的脚本来解决所有这些问题吗?感谢您提前的帮助

I need a little help reading specific values into a dictionary using Python. I have a csv file with User numbers. So user 1,2,3... each user is within a specific department 1,2,3... and each department is in a specific building 1,2,3... So I need to know how can I list all the users in department one in building 1 then department 2 in building 1 so on. I have been trying and have read everything into a massive dictionary using csv.ReadDict, but this would work if I could search through which entries I read into each dictionary of dictionaries. Any ideas for how to sort through this file? The CSV has over 150,000 entries for users. Each row is a new user and it lists 3 attributes, user_name, departmentnumber, department building. There are a 100 departments and 100 buildings and 150,000 users. Any ideas on a short script to sort them all out? Thanks for your help in advance

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

┾廆蒐ゝ 2024-10-23 08:05:40

蛮力方法看起来

import csv
csvFile = csv.reader(open('myfile.csv'))
data = list(csvFile)
data.sort(key=lambda x: (x[2], x[1], x[0]))

可以扩展到

import csv
import collections

csvFile = csv.reader(open('myfile.csv'))
data = collections.defaultdict(lambda: collections.defaultdict(list))

for name, dept, building in csvFile:
    data[building][dept].append(name)

buildings = data.keys()
buildings.sort()
for building in buildings:
    print "Building {0}".format(building)

    depts = data[building].keys()
    depts.sort()
    for dept in depts:
        print "  Dept {0}".format(dept)

        names = data[building][dept]
        names.sort()
        for name in names:
            print "   ",name

A brute-force approach would look like

import csv
csvFile = csv.reader(open('myfile.csv'))
data = list(csvFile)
data.sort(key=lambda x: (x[2], x[1], x[0]))

It could then be extended to

import csv
import collections

csvFile = csv.reader(open('myfile.csv'))
data = collections.defaultdict(lambda: collections.defaultdict(list))

for name, dept, building in csvFile:
    data[building][dept].append(name)

buildings = data.keys()
buildings.sort()
for building in buildings:
    print "Building {0}".format(building)

    depts = data[building].keys()
    depts.sort()
    for dept in depts:
        print "  Dept {0}".format(dept)

        names = data[building][dept]
        names.sort()
        for name in names:
            print "   ",name
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文