Skip to content

Instantly share code, notes, and snippets.

@shimizukawa
Created February 16, 2012 06:36
Show Gist options
  • Save shimizukawa/1842688 to your computer and use it in GitHub Desktop.
Save shimizukawa/1842688 to your computer and use it in GitHub Desktop.
Pythonコードを任意のエンコーディングに変換。#がコメント行として扱われるエンコーディングなら動作する。
#coding: base64
aW1wb3J0IHN5cwpzeXMuc3Rkb3V0LndyaXRlKCJIYXJ1byBXb3JsZCFcbiIpCg==
import sys
prog, fname, codec = sys.argv
r = open(sys.argv[1]).read()
w = r.encode(codec)
f = open("%(codec)s-%(fname)s" % locals(), 'wt')
f.write('#coding: %(codec)s\n' % locals())
f.write(w)
$ python hello.py
Haruo World!
$ python conv.py hello.py rot13
$ python rot13-hello.py
Haruo World!
$ python base64-hello.py
File "base64-hello.py", line 2
SyntaxError: bad argument type for built-in operation
$
import sys
sys.stdout.write("Haruo World!\n")
#coding: rot13
vzcbeg flf
flf.fgqbhg.jevgr("Unehb Jbeyq!\a")
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment