目录:
urlencode & quote & unquote (url 中带中文参数)
python httplib urllib urllib2区别(一撇)
当url地址含有中文或者“/”的时候,这是就需要用做urlencode一下编码转换。
1)其中python2.x需要urllib.urlencode()函数 (python3是urllib.parse.urlencode()函数);
2)urlencode解决的是key-value字典形式的参数(post的request格式)编码; 若是对单一的字符串进行转化的,得使用urllib.quote()函数
3)与urlencode 或 quote函数对应的函数是urllib.unquote()函数,进行解码
4)其它的re python正则问题
>>> import urllib
>>> data = {}
>>> data
{}
>>> data["key"] = "12345agsafgsdf"
>>> data
{'key': '12345agsafgsdf'}
>>> data["adrr"] = "北京昌平"
>>> data
{'adrr': '\xb1\xb1\xbe\xa9\xb2\xfd\xc6\xbd', 'key': '12345agsafgsdf'}
>>> data["电话"] = 136
>>> data
{'adrr': '\xb1\xb1\xbe\xa9\xb2\xfd\xc6\xbd', '\xb5\xe7\xbb\xb0': 136, 'key': '12345agsafgsdf'}
>>> profile={}
>>> profile["age":25]
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
TypeError: unhashable type
>>> profile["age"] = 25
>>> profile["sex"] = "男"
>>> profile["身高"] = 175
>>> profile
{'age': 25, '\xc9\xed\xb8\xdf': 175, 'sex': '\xc4\xd0'}
>>> data["profile"] = profile
>>> data
{'profile': {'age': 25, '\xc9\xed\xb8\xdf': 175, 'sex': '\xc4\xd0'}, 'adrr': '\xb1\xb1\xbe\xa9\xb2\xfd\xc6\xbd', '\xb5\xe7\xbb\xb0': 136, 'key': '12345agsafgsdf'}
>>> data_en = urllib.urlencode(data)
>>> data_en
'profile=%7B%27age%27%3A+25%2C+%27%5Cxc9%5Cxed%5Cxb8%5Cxdf%27%3A+175%2C+%27sex%27%3A+%27%5Cxc4%5Cxd0%27%7D&adrr=%B1%B1%BE%A9%B2%FD%C6%BD&%B5%E7%BB%B0=136&key=12345agsafgsdf'
>>> urllib.unquote(data_en)
"profile={'age':+25,+'\\xc9\\xed\\xb8\\xdf':+175,+'sex':+'\\xc4\\xd0'}&adrr=\xb1\xb1\xbe\xa9\xb2\xfd\xc6\xbd&\xb5\xe7\xbb\xb0=136&key=12345agsafgsdf"
>>>
对比import json
>>> profile_quote = urllib.quote(json.dumps(profile,encoding="gbk"))
>>> profile_quote
'%7B%22age%22%3A%2025%2C%20%22%5Cu8eab%5Cu9ad8%22%3A%20175%2C%20%22sex%22%3A%20%22%5Cu7537%22%7D'
>>> data["profile"] = profile_quote
>>> data
{'profile': '%7B%22age%22%3A%2025%2C%20%22%5Cu8eab%5Cu9ad8%22%3A%20175%2C%20%22sex%22%3A%20%22%5Cu7537%22%7D', 'adrr': '\xb1\xb1\xbe\xa9\xb2\xfd\xc6\xbd', '\xb5\xe7\xbb\xb0': 136, 'key': '12345agsafgsdf'}
>>> data_en = urllib.urlencode(data)
>>> urllib.unquote(data_en)
'profile=%7B%22age%22%3A%2025%2C%20%22%5Cu8eab%5Cu9ad8%22%3A%20175%2C%20%22sex%22%3A%20%22%5Cu7537%22%7D&adrr=\xb1\xb1\xbe\xa9\xb2\xfd\xc6\xbd&\xb5\xe7\xbb\xb0=136&key=12345agsafgsdf'
>>> data_en
'profile=%257B%2522age%2522%253A%252025%252C%2520%2522%255Cu8eab%255Cu9ad8%2522%253A%2520175%252C%2520%2522sex%2522%253A%2520%2522%255Cu7537%2522%257D&adrr=%B1%B1%BE%A9%B2%FD%C6%BD&%B5%E7%BB%B0=136&key=12345agsafgsdf'
urlencode的参数是词典,它可以将key-value这样的键值对转换成我们想要的格式。如果你用的是python2.*,urlencode在urllib.urlencode。如果使用的是python3,urlencode在urllib.parse.urlencode
例如
[python] view plain copy
1. import urllib.parse
2.
3. data={"name":"王尼玛","age":"/","addr":"abcdef"}
4. print(urllib.parse.urlencode(data))
输出为
[python] view plain copy
1. addr=abcdef&name=%E7%8E%8B%E5%B0%BC%E7%8E%9B&age=%2F
如果只想对一个字符串进行urlencode转换,怎么办?urllib提供另外一个函数:quote()
[python] view plain copy
1. print(urllib.parse.quote("hahaha你好啊!"))
输出为
[python] view plain copy
1. hahaha%E4%BD%A0%E5%A5%BD%E5%95%8A%EF%BC%81
当urlencode之后的字符串传递过来之后,接受完毕就要解码了——urldecode。urllib提供了unquote()这个函数,可没有urldecode()!
[python] view plain copy
1. import urllib.parse
2.
3. data={"name":"王尼玛","age":"/","addr":"abcdef"}
4. print(urllib.parse.urlencode(data))
5. print(urllib.parse.quote("hahaha你好啊!"))
6. print(urllib.parse.unquote("hahaha%E4%BD%A0%E5%A5%BD%E5%95%8A%EF%BC%81"))
输出
[python] view plain copy
1. addr=abcdef&name=%E7%8E%8B%E5%B0%BC%E7%8E%9B&age=%2F
2. hahaha%E4%BD%A0%E5%A5%BD%E5%95%8A%EF%BC%81
3. hahaha你好啊!
在做urldecode的时候,看unquote()这个函数的输出,是对应中文在gbk下的编码,在对比一下quote()的结果不难发现,所谓的urlencode就是把字符串转车gbk编码,然后把\x替换成%。如果你的终端是utf8编码的,那么要把结果再转成utf8输出,否则就乱码。
可以根据实际情况,自定义或者重写urlencode()、urldecode()等函数。
pythonre.search 和 re.match 正则表达式
一 re.search 和 re.match
python提供了2中主要的正则表达式操作:re.match 和 re.search。
match :只从字符串的开始与正则表达式匹配,匹配成功返回matchobject,否则返回none;
search :将字符串的所有字串尝试与正则表达式匹配,如果所有的字串都没有匹配成功,返回none,否则返回matchobject;(re.search相当于perl中的默认行为)
实例代码:
importre
deftestsearchandmatch():
s1="helloworld, i am 30 !"
w1 = "world"
m1 = re.search(w1, s1)
if m1:
print("find : %s" % m1.group())
if re.match(w1, s1) == none:
print("cannot match")
w2 = "helloworld"
m2 = re.match(w2, s1)
if m2:
print("match : %s" % m2.group())
testsearchandmatch()
#find : world
#cannot match
#match : helloworld
二 re.compile 和 re.ignorecase
re.compile返回regrexobject对象, 用来重复使用regrexobject;
re.ignorecase用来在匹配时忽略大小写;
实例代码:
deftestcompile():
regex = "d{3}-d{7}"
regexobject = re.compile(regex)
print(regexobject.search("aaa 027-4567892").group())
print(regexobject.search("bbb 021-1234567").group())
print(regexobject.search("ccc 010-123456"))
testcompile()
#027-4567892
#021-1234567
#none
deftestignorecase():
print(re.search('world', "hello world !").group())
print(re.search('world', "hello world !",re.ignorecase).group())
print(re.search('world', "hello world !"))
testignorecase()
#world
#world
#none
三 matchobject
matchobject为re.search,re.match等匹配成功后返回的对象,包含了匹配的结果。
在正则表达式中,可以使用()来将部分正则表达式分组且编号,编号从1开始,使用数字来使用,例如1 2 3,(?p<name>)还可以给分组命名, 使用(?p=name)来使用命名的组。
matchobject.group()包含了所有匹配的内容,等价于matchobject.group(0),此时的0表示所有的匹配;
matchobject.groups教程()包含了正则表达式中所有使用()定义的组对应的匹配内容;
matchobject.group(n),表示返回正则表达式中的第n个组()匹配的内容,此时n不为0, 等价于matchobject.groups()[n-1];
matchobject.lastindex,表示正则表达式中分组()的个数;
实例代码:
deftestmatchobject():
m =re.match(r"(?p<year>d{4})-(?p<month>d{2})-(?p<date>d{2})","2010-10-01, i am very happy")
print(m.group())
print(m.group(0))
print(m.groups())
print(m.group(1))
print(m.groups()[0])
print(m.group(2))
print(m.groups()[1])
print(m.group(3))
print(m.groups()[2])
print(m.groupdict())
print(m.lastindex)
testmatchobject()
#2010-10-01
#2010-10-01
#('2010', '10', '01')
#2010
#2010
#10
#10
#01
#01
#{'date': '01', 'year': '2010', 'month': '10'}
#3
四 re和matchobject的方法split+findall+finditer+sub
split方法,使用给定的表达式来分割字符串;
findall方法,返回所有的与给定的表达式匹配的一个list;
finditer方法,返回所有与给定的表达式匹配的matchobject的iterator;
sub方法,使用新的字符串替换表达式匹配的字符串;