gpt4 book ai didi

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

转载 作者:塔克拉玛干 更新时间:2023-11-03 05:37:56 25 4
gpt4 key购买 nike

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

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

最佳答案

您可以打开文件,将其放入列表,然后将所有用户放入某种哈希表

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

关于python - 在 Python 中对文件内容进行排序,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28228238/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com