python用法笔记

文件读写

(1)with...open

with open('feature.txt','w') as f1:
    for i in test['instance_id']:
        f1.write(str(i)+'\n')

with open('feature.txt','r') as f2:
    print(f2.read())

(2)pickle

import pickle
pickle.dump(df.columns.tolist(), open('feature','wb'))

feature_file = open('feature', 'rb')
feature_info= pickle.load(feature_file)
feature_info

(3)json


插入删除list/dict

>>> aa=[3,1,6]
>>> aa.pop()
6
>>> aa
[3, 1]
>>> bb=[33,11,66]
>>> bb.pop(0)
33
>>> bb
[11, 66]
>>> dic={'a':3,'b':1,'c':6}
>>> dic
{'a': 3, 'b': 1, 'c': 6}
>>> dic.pop('a')
3
>>> dic
{'b': 1, 'c': 6}
>>> ee=['A','B','C']
>>> ee.insert(1,10)
>>> ee
['A', 10, 'B', 'C']
最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容