将 Perl 翻译成 Python [英] Translating Perl to Python

查看:49
本文介绍了将 Perl 翻译成 Python的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在将我的 SQLite 数据库迁移到mysql

我想知道(因为我不懂 Perl)如何用 Python 重写它?

最短(代码)答案的奖励积分:)

编辑:抱歉,我指的是最短的代码,而不是严格意义上的最短答案

#!/usr/bin/perl而 ($line = <>){if (($line !~/BEGIN TRANSACTION/) && ($line !~/COMMIT/) && ($line !~/sqlite_sequence/) && ($line !~/CREATE UNIQUE指数/)){if ($line =~/CREATE TABLE \"([a-z_]*)\"(.*)/){$name = $1;$sub = $2;$sub =~ s/\"//g; #"$line = "如果存在 $name 则删除表;\n如果不存在则创建表 $name$sub\n";}elsif ($line =~/INSERT INTO \"([a-z_]*)\"(.*)/){$line = "插入 $1$2\n";$line =~ s/\"/\\\"/g;#"$line =~ s/\"/\'/g; #"}别的{$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;打印 $line;}}

成功迁移sqlite数据库需要一些额外的代码(处理一行Create table语句,外键,修复了原始程序中将空字段''转换为\'.

在迁移我的 SQLite 时发布了代码数据库转mysql问题

解决方案

这是一个非常直白的翻译,只有最少的明显样式更改(将所有代码放入一个函数中,尽可能使用字符串而不是重新操作).

>

import re, fileinput定义主():对于 fileinput.input() 中的行:过程=假因为在 ('BEGIN TRANSACTION','COMMIT','sqlite_sequence','创建唯一索引'):如果没有排队:中断别的:过程 = 真如果没有处理:继续m = re.search('CREATE TABLE "([a-z_]*)"(.*)', line)如果米:名称,子 = m.groups()line = '''DROP TABLE IF EXISTS %(name)s;如果不存在,则创建表 %(name)s%(sub)s'''line = line % dict(name=name, sub=sub)别的:m = re.search('INSERT INTO "([a-z_]*)"(.*)', line)如果米:行 = '插入 %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')印刷线,主要的()

I found this Perl script while migrating my SQLite database to mysql

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

解决方案

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()

这篇关于将 Perl 翻译成 Python的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆