1、保存列表為.txt文件
1
2
3
4
5
6
7
8
|
#1/list寫入txt ipTable = [ '158.59.194.213' , '18.9.14.13' , '58.59.14.21' ] fileObject = open ( 'sampleList.txt' , 'w' ) for ip in ipTable: fileObject.write(ip) fileObject.write( '\n' ) fileObject.close() |
2、字典保存
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
|
#2/dict寫入json import json dictObj = { 'andy' :{ 'age' : 23 , 'city' : 'shanghai' , }, 'william' : { 'age' : 33 , 'city' : 'hangzhou' , 'skill' : 'js' } } jsObj = json.dumps(dictObj) fileObject = open ( 'jsonFile.json' , 'w' ) fileObject.write(jsObj) fileObject.close() |
以上這篇python保存數(shù)據(jù)到本地文件的方法就是小編分享給大家的全部內(nèi)容了,希望能給大家一個參考,也希望大家多多支持服務(wù)器之家。
原文鏈接:https://blog.csdn.net/rosefun96/article/details/78877452