Last Updated: February 25, 2016
·
1.676K
· btv

Python CSV can delimit more than commas

You can use Python's csv module to parse more than just comma delimited data. By using the delimiter argument to have it fit almost any situation. For example reading "/etc/passwd" (using Ipython and python3):

In [1]: import csv

In [2]: data = csv.reader(open("/etc/passwd"), delimiter=':')

In [3]: for d in data:
    ...:     print(d)
    ...:     
['root', 'x', '0', '0', 'root', '/root', '/bin/bash']
['bin', 'x', '1', '1', 'bin', '/bin', '/bin/false']
['daemon', 'x', '2', '2', 'daemon', '/sbin', '/bin/false']
['mail', 'x', '8', '12', 'mail', '/var/spool/mail', '/bin/false']
['ftp', 'x', '14', '11', 'ftp', '/srv/ftp', '/bin/false']
['http', 'x', '33', '33', 'http', '/srv/http', '/bin/false']
...

You get the idea.