Translating Perl to Python

前端 未结 8 1500
[愿得一人]
[愿得一人] 2021-01-30 07:50

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 point

8条回答
  •  暖寄归人
    2021-01-30 08:01

    Based on http://docs.python.org/dev/howto/regex.html ...

    1. Replace $line =~ /.*/ with re.search(r".*", line).
    2. $line !~ /.*/ is just !($line =~ /.*/).
    3. Replace $line =~ s/.*/x/g with line=re.sub(r".*", "x", line).
    4. Replace $1 through $9 inside re.sub with \1 through \9 respectively.
    5. Outside a sub, save the return value, i.e. m=re.search(), and replace $1 with the return value of m.group(1).
    6. For "INSERT INTO $1$2\n" specifically, you can do "INSERT INTO %s%s\n" % (m.group(1), m.group(2)).

提交回复
热议问题