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

yipeiwu_com6年前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() 

以上这篇python保存数据到本地文件的方法就是小编分享给大家的全部内容了,希望能给大家一个参考,也希望大家多多支持【听图阁-专注于Python设计】。

相关文章

浅谈python中get pass用法

浅谈python中get pass用法

python中getpass 模块的作用是输入密码不可见 运行到这脚本不继续运行下去, 打开pycharm中的terminal 如上图显示,password中有输入密码,但不显示 以...

使用python开发vim插件及心得分享

使用python开发vim插件及心得分享

vim有各种强大的插件,这不仅归功于其提供的用来编写插件的脚本语言vimL,还得益于它良好的接口实现,从而支持python等语言编写插件。当vim编译时带有+python特性时就能使用p...

PyTorch中Tensor的拼接与拆分的实现

拼接张量:torch.cat() 、torch.stack() torch.cat(inputs, dimension=0) → Tensor 在给定维度上对输入的张量序列 s...

python3.4下django集成使用xadmin后台的方法

python3.4下django集成使用xadmin后台的方法

环境:window7 x64、python3.4、django1.10 一、pip install xadmin安装报错 1、使用pip install xadmin命令安装可能报如下错...

python程序控制NAO机器人行走

最近重新学习nao的官方文档,写点简单的程序回顾一下。主要是用python调用api,写下来保存着。 '''Walk:small example to make nao walk''...