python笔记(九)

60、Python3中的encode('unicode-escape')和encode('raw_unicode_escape')

  若某字符串的内容为bytes形式, 如 a = '\xe7\x8e\x8b\xe8\x80\x85\xe5\x86\x9c\xe8\x8d\xaf'

  可使用encode('raw_unicode_escape')将此str转化为bytes, 再decode为str

  可使用decode('raw_unicode_escape')输出内容为bytes形式的字符串

a = '\xe7\x8e\x8b\xe8\x80\x85\xe5\x86\x9c\xe8\x8d\xaf'

b = a.encode('raw_unicode_escape')

b.decode()'王者农药'

'王者农药'

若某字符串的内容为unicode形式, 如s = '\u5403\u9e21\u6218\u573a', 在py3中默认为utf-8编码, py3将其自动解释为 '吃鸡战场' 

  encode('unicode-escape')可将此str编码为bytes类型, 内容则是unicode形式

   decode('unicode-escape')可将内容为unicode形式的bytes类型转换为str

a = '\u5403\u9e21\u6218\u573a'>>> b = a.encode('unicode-escape')>>> type(b)<class 'bytes'>>>> b

b'\\u5403\\u9e21\\u6218\\u573a'>>> >>> b.decode('utf-8')'\\u5403\\u9e21\\u6218\\u573a'>>> >>> >>> c = b.decode('utf-8')>>> c'\ >>> c.encode().decode('unicode-escape')'吃鸡战场'

61、线程跟进程

进程:

优点:同时利用多个CPU,能够同时进行多个操作

缺点:耗费资源(重新开辟内存空间)

线程:

优点:共享内存,IO操作时候,创造并发操作

缺点:抢占资源

进程不是越多越好,CPU个数=进程个数;

线程也不是越多越好,具体案例具体分析,请求上下文切换耗时多的时候使用线程。

计算机中执行任务的最小单元:线程

IO密集型(不用CPU):多线程,计算密集型(用CPU):多进程。

62、常用高阶函数

1)counter,用于统计一个列表中每个元素出现的次数,类似于字典操作

import collections

c = collections.Counter(‘abcdgdagdcddgdaa’)

print(c.items()) # dict_items([('d', 6), ('a', 4), ('b', 1), ('g', 3), ('c', 2)])

print(c.most_common(3)) # [('d', 6), ('a', 4), ('g', 3)]
print(c.values()) # dict_values([1, 2, 3, 4, 6])

2)itertools.permutations,对列表中元素进行排列,参数可设置选择其中几个(排列是分元素顺序的)

p = itertools.permutations("abc",2)

# [('a', 'b'), ('a', 'c'), ('b', 'a'), ('b', 'c'), ('c', 'a'), ('c', 'b')]

3)itertools.combinations,对列表中元素进行组合,参数可设置选择其中几个(组合是不分元素顺序的)

c = itertools.combinations("abc",2)

[('a', 'b'), ('a', 'c'), ('b', 'c')]

4)itertools.product,创建一个迭代器,生成表示item1,item2等中的项目的笛卡尔积的元组,repeat是一个关键字参数,指定重复生成序列的次数

p=itertools.product([1,2,3],[8,9],repeat=1)

  # [(1, 8), (1, 9), (2, 8), (2, 9), (3, 8), (3, 9)]

5)zip,将多个迭代器的对应位置的元素组成一个个元组的列表,以最短的列表为准,多余的元素不组合。

z = zip([1,2,3],[4,5],[7,8])

 # [(1, 4, 7), (2, 5, 8)]

63、requests库上传文件

文件上传,在浏览器上查看参数,如下:其中“------WebKitFormBoundaryLlJ1aKLAx2BAxrx9”之间的数据为参数,name=”files”,name后面的字符为参数名称,之后为参数值。第一部分为文件参数,第二部分为form data类型的参数。

------WebKitFormBoundaryLlJ1aKLAx2BAxrx9

Content-Disposition: form-data; name="files"; filename="自定义表导入模板.xlsx"

Content-Type: application/vnd.openxmlformats-officedocument.spreadsheetml.sheet

------WebKitFormBoundaryLlJ1aKLAx2BAxrx9

Content-Disposition: form-data; name="dmExternalImport"

{"tableId":"373969459692470272","taskName":"ods_table_name_0924","projId":"200292042823204864","fieldMatchType":"0","isOverWrite":"Y","partitions":[{"name":"p_date","value":"213"}]}

--------WebKitFormBoundaryLlJ1aKLAx2BAxrx9

f = {“files”:open(r”C:\Users\10237221\Downloads\自定义表导入模板.xlsx”),”rb”}

Data = {“dmExternalImport”:’{"tableId":"373969459692470272","taskName":"ods_table_name_0924","projId":"200292042823204864","fieldMatchType":"0","isOverWrite":"Y","partitions":[{"name":"p_date","value":"213"}]}’}

response=requests.post(url = url,data = payload,files=files,headers=headers)

注意:1、打开文件函数中,一般为file或files,根据第一个区域中name=’files’或’files’来确定;2、第二个区域中,参数名为dmExternalImpor,参数值中看不出来是字典还是json,一种出错时,试另外一种。

59、python程序执行时寻找路径顺序

当你导入一个模块,Python解析器对模块位置的搜索顺序是:

● 当前目录

● 如果不在当前目录,Python 则搜索在 shell 变量 PYTHONPATH 下的每个目录。

● 如果都找不到,Python会察看默认路径。UNIX下,默认路径一般为/usr/local/lib/python/。

模块搜索路径存储在system模块的sys.path变量中。变量里包含当前目录,PYTHONPATH和由安装过程决定的默认目录

猜你喜欢

转载自www.cnblogs.com/yahutiaotiao/p/12688573.html