urlencode & quote & unquote (url 中帶中文參數)

目錄:

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

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  

二、unquote

當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方法,使用新的字符串替換表達式匹配的字符串;

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章