首页 > 技术文章 > python_笔记

dabaicai0703 2019-08-30 16:38 原文

1、url解析

  有时候获取到的url包含一些中文或者特殊符号,没有解析,显示形式例如:"https%3A%2F%2Fwww.baidu.com%2F"

  此时可以使用urllib库来解析

  from urllib.parse import unquote # from urllib.parse import quote,相反,quote可以对中文url进行编码

  str_old = "https%3A%2F%2Fwww.baidu.com%2F"

  str_new = unquote(str_old, "utf-8")

  print(str_new)  # 打印:https://www.baidu.com/

 

2、文件读取

  open("test.txt","r") # 如果test.txt中有中文,会报错

  open("test.txt","r",encoding="utf-8") # 设置好编码就正常了

  open("test.txt","r",encoding="utf-8",errors="ignore") # 加上errors参数,可以避免遇到非法字符时报错(遇到非法字符时,默认抛出异常,ignore)

推荐阅读