用冒号分隔的哟。
此章后面讲的JSON,配置文件读取,原理应该一样吧,只是要用合适的包去处理吧。
CSV文档是用CSV包处理的。
文档:
root:x:0:0:root:/root:/bin/bash
daemon:x:1:1:daemon:/usr/sbin:/bin/sh
bin:x:2:2:bin:/bin:/bin/sh
sys:x:3:3:sys:/dev:/bin/sh
sync:x:4:65534:sync:/bin:/bin/sync
games:x:5:60:games:/usr/games:/bin/sh
man:x:6:12:man:/var/cache/man:/bin/sh
lp:x:7:7:lp:/var/spool/lpd:/bin/sh
mail:x:8:8:mail:/var/mail:/bin/sh
news:x:9:9:news:/var/spool/news:/bin/sh
uucp:x:10:10:uucp:/var/spool/uucp:/bin/sh
proxy:x:13:13:proxy:/bin:/bin/sh
www-data:x:33:33:www-data:/var/www:/bin/sh
backup:x:34:34:backup:/var/backups:/bin/sh
代码:
import csv import sys from optparse import OptionParser if __name__ == '__main__': parser = OptionParser() parser.add_option('-f', '--file', help="CSV Data File") opts, args = parser.parse_args() if not opts.file: parser.error('File name not exist.') #Create a dict reader from an open file #handle and iterate through rows. csv.register_dialect('passwd', delimiter=':',quoting=csv.QUOTE_NONE) dict_key = ('login','pwd','uid','gid','comment','home','shell') reader = csv.DictReader(open(opts.file,'rU'),fieldnames=dict_key,dialect='passwd') for user in reader: print ('%s logs in with %s' % \ (user['login'], user['shell'])) csv.register_dialect
截图: