Quick HTML Table Creater

rory

T^T
is a Site Content Manager Alumnusis a Battle Simulator Moderator Alumnus
I really made this for my own personal use (but felt it would be worthwhile to share), so if something bothers you about it, I may fix it or I may not care.

If you don't have python, mysql and Veekun's pokedex in your mysql, this will not work for you.

To get Veekun's pokedex go here.

This script is used to create tables for articles. The table you create will have a Pokemon column, in addition to a column for each stat you specify. It will have a row for every Pokemon you input. It should be run from the command line. You obviously will need to update the conninfo in the beginning.

Code:
import MySQLdb

conninfo = { 'host'     : 'localhost' ,
             'user'     : 'root'      ,
             'passwd'   : 'password'  ,
             'db'       : 'pokedex'   }

trans = { 'HP'  : 'hp' ,
          'Atk' : 'at' ,
          'Def' : 'de' ,
          'SpA' : 'sa' ,
          'SpD' : 'sd' ,
          'Spe' : 'sp' }


def make_head(stats):

  head = []

  head.append( '  <thead>' )
  head.append( '    <tr class="a">' )
  head.append( '      <th>Pok&eacute;mon </th>' )

  for stat in stats:
    head.append( '      <th>%s </th>' % stat )

  head.append( '    </tr>' )
  head.append( '  </thead>' )

  return head

def make_body(data):

  a = 0
  body = []

  body.append( '  <tbody>' )

  for e in data:

    if not a:
      body.append( '    <tr>'  )
      a = 1
    else:
      body.append( '    <tr class="a">' )
      a = 0

    for c in e:
      body.append( '      <td>%s </td>' % c )

    body.append( '    </tr>' )

  body.append( '  </tbody>' )

  return body

def make_table(stats, body_data):

  table = []

  table.append( '<table class="sortable">' )
  table += make_head(stats)
  table += make_body(body_data)
  table.append( '</table>' )

  return table

def check_poke(poke):

  cursor = conn.cursor()
  cursor.execute("SELECT name FROM pokemon WHERE name = '%s';" % poke)
  row = cursor.fetchone()
  if row: return row[0]
  else: return row

def populate_stats(stats, pokemon):

  data = []
  cursor = conn.cursor()
  stats = ['stat_%s'%x for x in stats]
  stats = ', '.join(stats)

  for poke in pokemon:
    cursor.execute("SELECT %s FROM pokemon WHERE name = '%s';" % (stats, poke))
    row = cursor.fetchone()
    row = list(row)
    data.append([poke] + row)

  cursor.close()

  return data

def main():

  global conn

  print 'Connecting to mysql...'
  conn = MySQLdb.connect(**conninfo)
  print 'Connection successful!'

  print 'Which stats would you like to include in your table?'
  print 'Correct format: HP, Atk, Def, SpA, SpD, Spe (separated by commas, case sensitive)'
  stats = raw_input('stats?> ')
  stats = [x.strip() for x in stats.split(',')]
  dbstats = []
  for x in range(len(stats)):
    if stats[x] not in ('HP','Atk','Def','SpA','SpD','Spe'):
      print 'ERROR: Illegal stat: %s' % stats[x]
      return
    dbstats.append(trans[stats[x]])
  print 'Stats accepted!'

  print 'Now you may input Pokemon. Use a new line for each one. Hit enter on a blank line when finished.'
  pokemon = []
  while 1:
    poke = raw_input('pokemon?> ')
    if not poke: break
    poke = check_poke(poke)
    if poke:
      pokemon.append(poke)
      print '%s added.' % poke
    else:
      print 'Invalid input. Please try again.'
  print 'Pokemon added!'

  print 'Populating stats...'
  body_data = populate_stats(dbstats, pokemon)
  print 'Stats populated!'

  print 'Creating table...'
  table = make_table(stats, body_data)
  print 'Table created!'

  print 'Print to file? Input the filename. If none is input, it will print the output to this terminal (not recommended).'
  while 1:
    filename = raw_input('file?> ')
    if not filename:
      for line in table:
        print line
      break
    try:
      f = open(filename, 'w')
    except:
      print 'File could not be opened. Please try a different file'
    else:
      print 'Writing file...'
      for line in table:
        f.write(line + '\n')
      print 'File successfully written!'
      break

  print raw_input('Press enter to exit')

if __name__ == '__main__':

  main()
 

Users Who Are Viewing This Thread (Users: 1, Guests: 0)

Top