RubyからPythonへの乗り換え
参考になるサイト
- PythonRecipe Rubyレシピブックを置き換え
- 配列操作の比較表
対応するパッケージとか言葉
| Ruby | Python |
| gem | easy_install(setup_tools) |
| exerb | bbfreeze,py2exe, pyinstaller |
| RAA | PyPI |
| irb | IPython |
| htree | BeautifulSoup |
組み込みファンクションなどなど
| Ruby | Python |
| puts "a" | print "a" |
| print "a" | print "hoge", |
| a.sub!(/\n/,"") | a=re.sub("\n","",a) |
| a.push(b) | a.append(b) |
| if a=~/\n/ | if re.match("\n") |
| ARGV[0] | sys.argv[1] |
コード例
カレントのファイル一覧を取得
Ruby:
Dir.glob("./*")
Python:
listdir(".")
フィルタ
Ruby:
of=open(OUTFILE,"w") open(INFILE).each do |f| of.print f.gsub(/\n/,"") end
Python:
import re
of=open(OUTFILE,"w")
for f in open(INFILE):
f=re.sub("\n","",f,0)
of.write(f)
glob
Ruby:
Dir.glob("*.jpg").each do |f|
hoge(f)
end
Python:
import glob
for f in glob.glob("*.jpg"):
hoge(f)
html取得&表示
Ruby:
require "open-uri"
require "kconv"
doc=open("http://ashitani.jp").read
puts Kconv.toutf8(doc) #コード推測あり
Python:
from urllib import urlopen
doc=urlopen("http://ashitani.jp").read()
print doc.decode('euc-jp') #コード推測しない