<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0" xmlns:dc="http://purl.org/dc/elements/1.1/">
  <channel>
    <title>DZone Snippets: Druid's Code Snippets</title>
    <link>http://snippets.dzone.com/posts</link>
    <pubDate>Wed, 23 Jul 2008 18:56:44 GMT</pubDate>
    <description>DZone Snippets: Druid's Code Snippets</description>
    <item>
      <title>basic parser</title>
      <link>http://snippets.dzone.com/posts/show/4815</link>
      <description>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&lt;br /&gt;&lt;br /&gt;&lt;code&gt;&lt;br /&gt;def joinIds(fin, fout):&lt;br /&gt;    """reads a page from input file, joins Ids with ':'&lt;br /&gt;    &lt;br /&gt;    returns strings to output file, max of 50 ids in each"""&lt;br /&gt;    lines = 0&lt;br /&gt;    outlist = []&lt;br /&gt;    for line in fin:&lt;br /&gt;      if line.strip() != '':&lt;br /&gt;        lines += 1&lt;br /&gt;        outlist.append(line.split()[2])&lt;br /&gt;        if lines % 50 == 0:&lt;br /&gt;            fout.write(":".join(["%s" % Id for Id in outlist]) + "\n\n")&lt;br /&gt;            outlist = []&lt;br /&gt;    fout.write(":".join(["%s" % Id for Id in outlist]) + "\n\n")&lt;br /&gt;&lt;br /&gt;try:&lt;br /&gt;    SIA = raw_input('Enter a file to read: ')&lt;br /&gt;    pages = open(SIA, 'r')&lt;br /&gt;except IOError:&lt;br /&gt;    print 'Cannot open file %s for reading. Check file exists and try again.' % SIA&lt;br /&gt;    import sys&lt;br /&gt;    sys.exit(0)&lt;br /&gt;    &lt;br /&gt;&lt;br /&gt;default = 'idFile.txt'&lt;br /&gt;out = raw_input('File name for output: ')&lt;br /&gt;if out == '':&lt;br /&gt;    oFile = open(default, 'a')&lt;br /&gt;else:&lt;br /&gt;    default = out&lt;br /&gt;    oFile = open(default, 'a')&lt;br /&gt;&lt;br /&gt;joinIds(pages, oFile)&lt;br /&gt;pages.close()&lt;br /&gt;oFile.close()&lt;br /&gt;&lt;br /&gt;print 'Id strings have been output to %s. Happy mass-mailing! ;)' % default&lt;br /&gt;&lt;/code&gt;</description>
      <pubDate>Sun, 25 Nov 2007 07:39:12 GMT</pubDate>
      <guid>http://snippets.dzone.com/posts/show/4815</guid>
      <author>druid (Geoff)</author>
    </item>
  </channel>
</rss>
