[Python][文字化け]
- 構文
quote(s, safe='/')
- 予約文字
reserved = gen-delims / sub-delims
gen-delims = ":" / "/" / "?" / "#" / "[" / "]" / "@"
sub-delims = "!" / "$" / "&" / "'" / "(" / ")"
/ "*" / "+" / "," / ";" / "="
- 非予約文字
unreserved = ALPHA / DIGIT / "-" / "." / "_" / "~"
- 区切り文字
delims = "<" | ">" | "#" | "%" | <">
- 安全でない
unwise = "{" | "}" | "|" | "\" | "^" | "[" | "]" | "`"
- 例1
>>> import urllib
>>> urllib.quote('abc def')
'abc%20def'
- 例2
>>> urllib.quote('http://test.com?p=abc def')
'http%3A//test.com%3Fp%3Dabc%20def'
- 例3
>>> urllib.quote('http://test.com?p=abc def', safe=';/?:@&=+$,')
'http://test.com?p=abc%20def'
- タプル、またはディクショナリを、URL クエリー文字列として生成する
>>> import urllib
>>> param = {'q':'test', 'id':'piroto'}
>>> urllib.urlencode(param)
'q=test&id=piroto'
HTML特殊文字のエスケープ、アンエスケープ
- xml.sax.saxutils の escape, unescape を利用すると便利
>>> from xml.sax.saxutils import *
>>> escape("<&>")
'<&>'
>>> unescape("<&>")
'<&>'
>>> unescape("'",{"'":"'"})
"'"
YAGI Hiroto (piroto@a-net.email.ne.jp)
twitter http://twitter.com/pppiroto
Copyright© 矢木 浩人 All Rights Reserved.