Download A Python Book - Dave Kuhlman dot org
Transcript
A Python Book def test(): args = sys.argv[1:] if len(args) != 1: print 'usage: python pyparsing_test3.py <datafile.txt>' sys.exit(1) infilename = sys.argv[1] infile = file(infilename, 'r') for line in infile: line = line.strip() if line and line[0] != "#": fields = record.parseString(line) print fields test() And, here is some sample input: Jabberer, Jerry 1112223333 Bakersfield, CA 95111 Kackler, Kerry 1112223334 Fresno, CA 95112 Louderdale, Larry 1112223335 Los Angeles, CA 94001 Here is output from parsing the above input: [['Jabberer', 'Jerry'], '1112223333', [['Bakersfield'], 'CA', '95111']] [['Kackler', 'Kerry'], '1112223334', [['Fresno'], 'CA', '95112']] [['Louderdale', 'Larry'], '1112223335', [['Los', 'Angeles'], 'CA', '94001']] Comments: ● ● ● ● We use the len=n argument to the Word constructor to restict the parser to accepting a specific number of characters, for example in the zip code and phone number. Word also accepts min=n'' and ``max=n to enable you to restrict the length of a word to within a range. We use Group to group the parsed results into sublists, for example in the definition of city and name. Group enables us to organize the parse results into simple parse trees. We use Combine to join parsed results back into a single string. For example, in the phone number, we can require dashes and yet join the results back into a single string. We use Suppress to remove unneeded subelements from parsed results. For example, we do not need the comma between last and first name. 2.6.6.4 A more complex example This example (thanks to Paul McGuire) parses a more complex structure and produces a dictionary. Page 151