美文网首页
python用法笔记

python用法笔记

作者: 拼搏向上001 | 来源:发表于2019-04-04 11:02 被阅读0次

文件读写

(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']

相关文章

网友评论

      本文标题:python用法笔记

      本文链接:https://www.haomeiwen.com/subject/wskfiqtx.html