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设计】。

相关文章

在Linux系统上部署Apache+Python+Django+MySQL环境

在Linux系统上部署Apache+Python+Django+MySQL环境

Linux+apache+mysql+python+mod_python+Django 说明:系统rhel 5.3,默认安装httpd、mysql,没有安装的,请下载安装RPM包,删除/...

Python函数中不定长参数的写法

Python函数中不定长参数的写法

1、不定长参数的写法,用 *变量名 表示 2、不定长参数累加 3、不定长参数,使用**c接受m=23,n=56的值; 传参时,a必写,b、c可以缺省 def fun(a, b,...

Python最基本的输入输出详解

输出 用print加上字符串,就可以向屏幕上输出指定的文字。比如输出'hello, world',用代码实现如下: >>> print 'hello, world'...

基于python socketserver框架全面解析

基于python socketserver框架全面解析

socketserver框架是一个基本的socket服务器端框架, 使用了threading来处理多个客户端的连接, 使用seletor模块来处理高并发访问, 是值得一看的python...

python中bs4.BeautifulSoup的基本用法

导入模块 from bs4 import BeautifulSoup soup = BeautifulSoup(html_doc,"html.parser") 下面看下常见的用...