目录:

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

urlencode quote unquote (url 中带中文参数)相关推荐

  1. java.net.url 中文乱码_asp.net URL中包含中文参数造成乱码的解决方法

    asp.net URL中包含中文参数造成乱码的解决方法 更新时间:2010年03月08日 21:44:27   作者: 中文乱码一直以来是WEB开发中比较常见的问题之一,对于初学者来说,各种各样的编码 ...

  2. Js的Url中传递中文参数乱码的解决

    一:Js的Url中传递中文参数乱码问题,重点:encodeURI编码,decodeURI解码: 1.传参页面 Javascript代码: 2. 接收参数页面:test02.html 二:如何获取Url ...

  3. url中传递中文参数时的转码与解码

    URL传递中文参数时的几种处理方式,总结如下: 1.将字符串转码:newString("xxxxx".getBytes("iso-8859-1")," ...

  4. 微信小程序获取二维码中URL中带的参数

    解析微信小程序获取二维码中的url参数 onLoad: function (options) { console.log(options) let qrUrl = decodeURIComponent ...

  5. URL传参时 从URL中获取中文参数的方法

    利用url传参时如果url中的参数是中文时因为编码类型不同在页面中获取会出现乱码 使用此方法能获取url中的参数值 并解决乱码问题 调用时直接 GetUrlByParamName("参数名& ...

  6. IE浏览器url带中文参数导致乱码问题(chrome下正常)

    问题: E浏览器下url带中文请求参数,服务器端使用new String(param.getBytes("iso-8859-1"), "utf-8")后仍然会乱 ...

  7. Java对URL中的中文进行UrlEncode转码

    Java对URL中的中文进行UrlEncode urleocode只是为了url中一些非ascii字符,可以正确无误的被传输. 而且有的含有中文或者特殊字符的url不能被识别,因此需要进行Encode ...

  8. 在URL中传递中文的解决方式

    在URL中传递中文的解决方式 2015年05月13日 17:13:55 thinkinglink 阅读数:10321 标签: URL编码中文编码乱码解决 更多 个人分类: web前端 在URL中传递中 ...

  9. linux minicon乱码,路径中带中文出现乱码问题

    路径中带中文出现乱码问题 來源:互聯網  2016-05-28 09:34:21  評論 路径中带中文出现乱码问题做项目的时候我们可能会遇到需要使用路径传参数的问题,如果参数是数字或者英文,那肯定不会 ...

最新文章

  1. 一个「PPT」框架,让超大模型调参变简单:清华刘知远、黄民烈团队力作
  2. sql盲注特点_SQL注入介绍及分类解读
  3. ORA-12545: Connect failed because target host or object does not exist
  4. jenkins自动化打包部署,jenkins执行sh脚本不退出问题
  5. 程序员幽默:工作群里常见表情的真正含义……
  6. iview render的时候可以写控件的基本格式
  7. Docker 安装 redis 、Redis docker 方式部署
  8. spring AOP自定义注解方式实现日志管理
  9. Redis之jedis连接池
  10. 《庆余年》里的五竹,到底是不是机器人?
  11. RocketMQ 拉取消息-通信模块
  12. vmware15安装‘黑苹果’macOS 10.13(17A365)
  13. 【Vue中的坑】Vue中的修改变量没有效果?
  14. linux结课考试试题,Linux认证考试课后基础试题及答案
  15. 通用技术和信息技术合格考知识点_通用技术学考复习_重要知识点汇总
  16. php简单文章,PHP简单实现“相关文章推荐”功能的方法
  17. 在themeforest购买主题获得激活码方法
  18. python解密码编程_python生成密码本 - 『编程语言区』 - 吾爱破解 - LCG - LSG |安卓破解|病毒分析|www.52pojie.cn...
  19. Beyond Compare 提示错误“这个授权密钥已被吊销”的解决办法
  20. android service是什么,Android service是什么 Android service详解

热门文章

  1. java test1
  2. Python中的self,类中的self?
  3. linux curl命令详解,以及实例
  4. 【网络】https单向认证和双向认证
  5. 博林格林大学计算机排名,美国传媒研究生排名简述
  6. IIS服务器安全配置[摘]
  7. platform_device 与 platform_driver 是如何匹配的?
  8. 凤凰新闻自动评论推荐软件--把自己的评论推荐到最前面,然后通过头像、用户名或者评论内容进行展示
  9. x86保护模式——全局描述符表GDT详解
  10. java程序性能优化(实例)