O'Reilly logo

Python 2.6 Text Processing Beginner's Guide by Jeff McNeil

Stay ahead with the world's most comprehensive technology and business learning platform.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, tutorials, and more.

Start Free Trial

No credit card required

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

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, interactive tutorials, and more.

Start Free Trial

No credit card required