Never been to DZone Snippets before?

Snippets is a public source code repository. Easily build up your personal collection of code snippets, categorize them with tags / keywords, and share them with the world

About this user

Geoff

« Newer Snippets
Older Snippets »
Showing 1-1 of 1 total  RSS 

basic parser

This is a very basic parser, reads each line and creates a string of ':'-separated id nos for mass-mailing a set of accounts in an online game

   1  
   2  def joinIds(fin, fout):
   3      """reads a page from input file, joins Ids with ':'
   4      
   5      returns strings to output file, max of 50 ids in each"""
   6      lines = 0
   7      outlist = []
   8      for line in fin:
   9        if line.strip() != '':
  10          lines += 1
  11          outlist.append(line.split()[2])
  12          if lines % 50 == 0:
  13              fout.write(":".join(["%s" % Id for Id in outlist]) + "\n\n")
  14              outlist = []
  15      fout.write(":".join(["%s" % Id for Id in outlist]) + "\n\n")
  16  
  17  try:
  18      SIA = raw_input('Enter a file to read: ')
  19      pages = open(SIA, 'r')
  20  except IOError:
  21      print 'Cannot open file %s for reading. Check file exists and try again.' % SIA
  22      import sys
  23      sys.exit(0)
  24      
  25  
  26  default = 'idFile.txt'
  27  out = raw_input('File name for output: ')
  28  if out == '':
  29      oFile = open(default, 'a')
  30  else:
  31      default = out
  32      oFile = open(default, 'a')
  33  
  34  joinIds(pages, oFile)
  35  pages.close()
  36  oFile.close()
  37  
  38  print 'Id strings have been output to %s. Happy mass-mailing! ;)' % default
« Newer Snippets
Older Snippets »
Showing 1-1 of 1 total  RSS