在Python中对文件的内容进行排序

时间:2015-01-30 02:21:02

标签: python algorithm file sorting

大家好我是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

我将非常感谢这个例子如何有效地完成或应用任何 排序算法。 谢谢!

2 个答案:

答案 0 :(得分:3)

你可以打开文件,把它扔进一个列表然后把所有用户都扔进某个哈希表

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)))

答案 1 :(得分:1)

这应该遍历您的/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))