Python 中的文件内容排序

Sorting content of the file in Python

大家好,我是 python 的新手,我正在尝试根据以下标准使用 PYTHON 3.4 对 /etc/passwd 文件进行排序: 在 linux 系统上输入(常规 /etc/passwd 文件:

raj:x:501:512::/home/raj:/bin/ksh
ash:x:502:502::/home/ash:/bin/zsh
jadmin:x:503:503::/home/jadmin:/bin/sh
jwww:x:504:504::/htdocs/html:/sbin/nologin
wwwcorp:x:505:511::/htdocs/corp:/sbin/nologin
wwwint:x:506:507::/htdocs/intranet:/bin/bash
scpftp:x:507:507::/htdocs/ftpjail:/bin/bash
rsynftp:x:508:512::/htdocs/projets:/bin/bash
mirror:x:509:512::/htdocs:/bin/bash
jony:x:510:511::/home/jony:/bin/ksh
amyk:x:511:511::/home/amyk:/bin/ksh

我要查找的输出到文件或返回到屏幕:

Group 511 : jony, amyk, wwwcorp
Group 512 : mirror, rsynftp, raj
Group 507 : wwwint, scpftp 
and so on 

这是我的计划:

1) Open and read the whole file or do it line by line 
2) Loop through the file using python regex 
3) Write it into temp file or create a dictionary 
4) Print the dictionary keys and values

我真的很感激这个例子如何有效地完成或应用任何 排序算法。 谢谢!

这应该遍历您的 /etc/passwd 并按组对用户进行排序。你不需要做任何花哨的事情来解决这个问题。

with open('/etc/passwd', 'r') as f:
    res = {}

    for line in f:
        parts = line.split(':')

        try:
            name, gid = parts[0], int(parts[3])
        except IndexError:
            print("Invalid line.")
            continue

        try:
            res[gid].append(name)
        except KeyError:
            res[gid] = [name]

for key, value in res.items():
    print(str(key) + ': ' + ', '.join(value))

您可以打开文件,将其放入一个列表中,然后将所有用户放入某种散列中 table

with open("/etc/passwd") as f:
    lines = f.readlines()

group_dict = {}
for line in lines:
    split_line = line.split(":")
    user = split_line[0]
    gid = split_line[3]
    # If the group id is not in the dict then put it in there with a list of users 
    # that are under that group
    if gid not in group_dict:
        group_dict[gid] = [user]
    # If the group id does exist then add the new user to the list of users in 
    # the group
    else:
        group_dict[gid].append(user)

# Iterate over the groups and users we found. Keys (group) will be the first item in the tuple, 
# and the list of users will be the second item. Print out the group and users as we go
for group, users in group_dict.iteritems():
    print("Group {}, users: {}".format(group, ",".join(users)))