Over a million developers have joined DZone.

Basic Parser

DZone's Guide to

Basic Parser

· ·
Free Resource
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

def joinIds(fin, fout):
    """reads a page from input file, joins Ids with ':'
    returns strings to output file, max of 50 ids in each"""
    lines = 0
    outlist = []
    for line in fin:
      if line.strip() != '':
        lines += 1
        if lines % 50 == 0:
            fout.write(":".join(["%s" % Id for Id in outlist]) + "\n\n")
            outlist = []
    fout.write(":".join(["%s" % Id for Id in outlist]) + "\n\n")

    SIA = raw_input('Enter a file to read: ')
    pages = open(SIA, 'r')
except IOError:
    print 'Cannot open file %s for reading. Check file exists and try again.' % SIA
    import sys

default = 'idFile.txt'
out = raw_input('File name for output: ')
if out == '':
    oFile = open(default, 'a')
    default = out
    oFile = open(default, 'a')

joinIds(pages, oFile)

print 'Id strings have been output to %s. Happy mass-mailing! ;)' % default

Opinions expressed by DZone contributors are their own.

{{ parent.title || parent.header.title}}

{{ parent.tldr }}

{{ parent.urlSource.name }}