使用Python过程中遇到的问题

Python 问题集

1.使用try...except...语句捕获异常时应尽量指定异常的具体类型。

2.format输出百分数的形式:

format(4.2/5.1,'.2%’) -> 82.35%

3.使用list.append()会返回一个Nonetype的类型,
可以使用两个list相加的方法。

4.Python文件读写:r-只读,w-只写,a-追加

5.Python url中包含中文,编码问题。使用urlencode()函数。

from urllib.parse import urlencode
app_name = '水果忍者'
parameters = {'key': app_name} # dict
key = urlencode(parameters)
search_url = 'http://www.wandoujia.com/search?'+key  # http://www.wandoujia.com/search?key=%E8%AF%B4%E5%95%A5

6.Mac上pip install装包出现权限错误时,可以使用这种方式:pip install --user

7.Python初始化二维数组:
multilist = [[0 for col in range(5)] for row in range(3)]
参考:http://www.cnblogs.com/coderzh/archive/2008/05/18/1201993.html

8.字典按value排序:
sorted(dict1, key=dict1.get)
理解key=dict1.get: http://stackoverflow.com/questions/39496096/understanding-dictionary-get-in-python/39496240

Python学习资料

1.Python Best Practices Guidebook :https://github.com/kennethreitz/python-guide

原文地址:https://www.cnblogs.com/binwone/p/6041613.html