网络编程
位置:首页>> 网络编程>> Python编程>> python保存数据到本地文件的方法

python保存数据到本地文件的方法

作者:rosefun96  发布时间:2023-10-01 20:59:53 

标签:python,数据,文件

1、保存列表为.txt文件


#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、字典保存


#2/dict写入json
import json

dictObj = {
'andy':{
 'age': 23,
 'city': 'shanghai',
 'skill': 'python'
},
'william': {
 'age': 33,
 'city': 'hangzhou',
 'skill': 'js'
}
}

jsObj = json.dumps(dictObj)

fileObject = open('jsonFile.json', 'w')
fileObject.write(jsObj)
fileObject.close()

来源:https://blog.csdn.net/rosefun96/article/details/78877452

0
投稿

猜你喜欢

手机版 网络编程 asp之家 www.aspxhome.com