Time for action - writing JSON data

Note that this requires Python 2.6. If you're using an earlier version, you'll need to install the simplejson library that's available via the Python Package Index. simply run easy_install simplesjson from within your virtual environment.

In this example, we'll update our CSV reader to output a list of UNIX users in our password file in JSON format.

  1. Create a new file named csv_reader-d.py. You can use csv_reader-c.py as a base, or create an empty file.
  2. Update the code in csv_reader-d.py to contain the following:
     import csv import sys import json from optparse import OptionParser if __name__ == '__main__': parser = OptionParser() parser.add_option('-f', '--file', help="CSV Data File") opts, args = parser.parse_args() ...

Get Python 2.6 Text Processing Beginner's Guide now with O’Reilly online learning.

O’Reilly members experience live online training, plus books, videos, and digital content from 200+ publishers.