ThinkChat2.0新版上线,更智能更精彩,支持会话、画图、视频、阅读、搜索等,送10W Token,即刻开启你的AI之旅 广告
[TOC] ## 代码技巧 ### 元组赋值 ``` s = ("hello",'word'); a,b=s print(a,b) #hello word ``` ### *args 和**kw的区别 ``` def name(*args, **kw): print(*args) # hello word print(args) # ('hello', 'word') print(*kw) # name age print(kw) # {'name': 'cpj', 'age': '12'} name('hello', 'word', name="cpj", age="12") ``` ### `__name__`作用 #打印入口文件 print(__name__) #__main__ # test3.py print(__name__) #在入口文件引入 test3 #输出:test3 ### 转码 ``` a = unicode.encode(u'慕课','utf-8') print(a) #b'\xe6\x85\x95\xe8\xaf\xbe ``` ### 特定编码写入或读取 ``` import codecs f = codecs.open("test.txt",'w','utf-8') ``` ### `__init__.py` 文件用法 目录结构 ``` Phone ├ Isdn.py ├ G3.py ├ _init__.py ``` _init__.py文件 ``` from Pots import Pots from Isdn import Isdn from G3 import G3 ``` ### 字典转json字符串 dict = {'name':'cpj','age':'12'} print(json.dumps(dict)) ### 同时遍历两个数组 ``` a = ['Pradeepto', 'Kushal'] b = ['OpenSUSE', 'Fedora'] for x, y in zip(a, b): print("{} uses {}".format(x, y)) ``` ### pyinstaller打包的exe太大 1. 使用 `virtualenv` 环境在环境里进行打包