Translating Perl to Python
Question: I found this Perl script while [migrating my SQLite database to
mysql](http://stackoverflow.com/questions/18671/quick-easy-way-to-migrate-
sqlite3-to-mysql/25860)

I was wondering (since I don't know Perl) how could one rewrite this in
Python?

Bonus points for the shortest (code) answer :)

**edit** : sorry I meant shortest code, not strictly shortest answer

    
    
    #! /usr/bin/perl
    
    while ($line = <>){
        if (($line !~  /BEGIN TRANSACTION/) && ($line !~ /COMMIT/) && ($line !~ /sqlite_sequence/) && ($line !~ /CREATE UNIQUE INDEX/)){
    
            if ($line =~ /CREATE TABLE \"([a-z_]*)\"(.*)/){
                    $name = $1;
                    $sub = $2;
                    $sub =~ s/\"//g; #"
                    $line = "DROP TABLE IF EXISTS $name;\nCREATE TABLE IF NOT EXISTS $name$sub\n";
            }
            elsif ($line =~ /INSERT INTO \"([a-z_]*)\"(.*)/){
                    $line = "INSERT INTO $1$2\n";
                    $line =~ s/\"/\\\"/g; #"
                    $line =~ s/\"/\'/g; #"
            }else{
                    $line =~ s/\'\'/\\\'/g; #'
            }
            $line =~ s/([^\\'])\'t\'(.)/$1THIS_IS_TRUE$2/g; #'
            $line =~ s/THIS_IS_TRUE/1/g;
            $line =~ s/([^\\'])\'f\'(.)/$1THIS_IS_FALSE$2/g; #'
            $line =~ s/THIS_IS_FALSE/0/g;
            $line =~ s/AUTOINCREMENT/AUTO_INCREMENT/g;
            print $line;
        }
    }
    

Some additional code was necessary to successfully migrate the sqlite database
(handles one line Create table statements, foreign keys, fixes a bug in the
original program that converted empty fields `''` to `\'`.

I [posted the code on the migrating my SQLite database to mysql
Question](http://stackoverflow.com/questions/18671/quick-easy-way-to-migrate-
sqlite3-to-mysql/1067365#1067365)


Answer: Here's a pretty literal translation with just the minimum of obvious style
changes (putting all code into a function, using string rather than re
operations where possible).

    
    
    import re, fileinput
    
    def main():
      for line in fileinput.input():
        process = False
        for nope in ('BEGIN TRANSACTION','COMMIT',
                     'sqlite_sequence','CREATE UNIQUE INDEX'):
          if nope in line: break
        else:
          process = True
        if not process: continue
        m = re.search('CREATE TABLE "([a-z_]*)"(.*)', line)
        if m:
          name, sub = m.groups()
          line = '''DROP TABLE IF EXISTS %(name)s;
    CREATE TABLE IF NOT EXISTS %(name)s%(sub)s
    '''
          line = line % dict(name=name, sub=sub)
        else:
          m = re.search('INSERT INTO "([a-z_]*)"(.*)', line)
          if m:
            line = 'INSERT INTO %s%s\n' % m.groups()
            line = line.replace('"', r'\"')
            line = line.replace('"', "'")
        line = re.sub(r"([^'])'t'(.)", r"\1THIS_IS_TRUE\2", line)
        line = line.replace('THIS_IS_TRUE', '1')
        line = re.sub(r"([^'])'f'(.)", r"\1THIS_IS_FALSE\2", line)
        line = line.replace('THIS_IS_FALSE', '0')
        line = line.replace('AUTOINCREMENT', 'AUTO_INCREMENT')
        print line,
    
    main()
    

