python学习三(数据保存到文件)
以写模式打开文件:需要指定写模式,如下所示
data = open('data.out','w')
如果文件已经存在,则会清空它现有的所有内容。要追加一个文件,需要使用访问模式a,会追加到下一行。
例子:将上节中Man和Other Man说的话,分别保存到两个文件中
man = []
other = [] try:
data = open('sketch.txt') for each_line in data:
try:
(role, line_spoken) = each_line.split(':')
line_spoken = line_spoken.strip()
if role == 'Man':
man.append(line_spoken)
elif role == 'Other Man':
other.append(line_spoken)
else:
pass
except ValueError:
pass data.close()
except IOError:
print('The datafile is missing!')
#使用print将列表中的数据输出到文件中
try:
with open('man_data.txt', 'w') as man_file, open('other_data.txt', 'w') as other_file:
print(man, file=man_file)
print(other, file=other_file)
except IOError as err:
print('File error: ' + str(err))
使用with open()方法保存文件,不需要再用close方法关闭文件
Python提供了一个标准库,名为pickle,它可以加载、保存几乎任何的Python数据对象,包括列表
使用python的dump保存,load恢复
import pickle man = []
other = [] try:
data = open('sketch.txt') for each_line in data:
try:
(role, line_spoken) = each_line.split(':')
line_spoken = line_spoken.strip()
if role == 'Man':
man.append(line_spoken)
elif role == 'Other Man':
other.append(line_spoken)
else:
pass
except ValueError:
pass data.close()
except IOError:
print('The datafile is missing!') try:
with open('man_data.txt', 'wb') as man_file, open('other_data.txt', 'wb') as other_file:
pickle.dump(man, file=man_file)
pickle.dump(other, file=other_file)
except IOError as err:
print('File error: ' + str(err))
except pickle.PickleError as perr:
print('Pickling error: ' + str(perr))
'wb'中的b表示以二进制模式打开文件
要读取二进制文件使用'rb'
import pickle
with open('man_data.txt','rb') as readText:
a_list = pickle.load(readText)
print(a_list)
python学习三(数据保存到文件)的更多相关文章
- Python 脚本生成测试数据,Python生成随机数据,Python生成大量数据保存到文件夹中
代码如下: import random import datetime import time dataCount = 10*100*100 #10M. codeRange = range(ord(' ...
- [Python] Python 学习 - 可视化数据操作(一)
Python 学习 - 可视化数据操作(一) GitHub:https://github.com/liqingwen2015/my_data_view 目录 折线图 散点图 随机漫步 骰子点数概率 文 ...
- python将控制台输出保存到文件
python将控制台输出保存到文件 在平时工作中,有时我们需要将控制台输出保存到文件 1.命令行用>覆盖写入和>>追加写入 for i in range(10000): prin ...
- Python学习笔记_Chapter 4数据保存到文件
1. What For 将基于内存的数据存储到磁盘上,达到持续存储. 2. HOW 方法一: 将数据写到文件中 常规的处理方式 #file.x被打开的文件,model打开文件的方式 out=open( ...
- Python 通过print_lol将数据保存到文件中
1. 定义一个print_lol函数来控制列表的缩进和写入位置 import sys """this is a new fuction, which work for a ...
- Python 通过print将数据保存到文件中
1. Print them to screen man = [] other = [] try: data = open('sketch.txt') for each_line in data: tr ...
- python将字典中的数据保存到文件中
d = {'a':'aaa','b':'bbb'}s = str(d)f = open('dict.txt','w')f.writelines(s)f.close()
- Python学习记录----数据定义
摘要: 描述Python中数据定义格式,需要注意的东东. 一 数据声明 Python木有一般语言的具体数据类型,像char,int,string这些通通木有.这有点像javascript,但又不同,j ...
- Python scrapy爬虫数据保存到MySQL数据库
除将爬取到的信息写入文件中之外,程序也可通过修改 Pipeline 文件将数据保存到数据库中.为了使用数据库来保存爬取到的信息,在 MySQL 的 python 数据库中执行如下 SQL 语句来创建 ...
随机推荐
- mysqllog
-- mysql delete log online 1 mysql命令purge mysql> purge master logs to "mysql-bin.000410&quo ...
- Azure blob Storage Snapshot
用户虚拟机硬盘的备份是客户在部署云应用中是一个非常重要的部分. 目前有多种平台的备份方法: 捕获镜像:可以采用Capture的方式(powershell命令为Save-AzureVMImage)捕获虚 ...
- 人物-IT-刘强东:刘强东
ylbtech-人物-IT-刘强东:刘强东 刘强东,男,汉族,1973年3月10日生(另一说法:1974年2月14日),江苏宿迁人,祖籍湖南湘潭 .京东集团董事局主席兼首席执行官,本科毕业于中国人民大 ...
- mybatis 学习二 conf xml 配置信息
xml映射配置文件 这个xml文件主要包括一下节点信息 * properties 属性 * settings 设置 * typeAliases 类型命名 ...
- Mach系统
——杂言:最近升级了把山狮升级到了10.9mavericks,比较抓我注意力的就是新的活动监视器.新的监视器里对统计分类很严,双击某一进程,即可跳出一个详情对话框.其中里面就有记录着Mach相关的信息 ...
- sharepoint SDDL 字符串包含无效的SID或无法转换的SID
安装过程中出现以下错误 采用独立模式安装Sharepoint Server 2013/Foundation 2013,在进行配置向导的时候会碰到这样的错误 System.ArgumentExcepti ...
- 微信小程序报错.wxss无法找到
小程序原来一直运行正常,编译都没有问题,但今天更新了一下工具,就一直编译不过,报.wxss无法找到,搜索半天,才解决. 解决方案如下: 在控制台输入openVendor(), 在打开的目录中清除wcs ...
- lyui 列表 上传
1.js layui.use(['table', 'element', 'laydate', 'layer','upload'], function () { var table = layui.ta ...
- Learning Python 010 函数 1
Python 函数 1 调用函数 举个例子 多于Python内部的函数,你可以在Python的交互式终端中使用help()函数来查看函数的使用方法.比如:abs()函数,如果你不知道如何使用它,你可以 ...
- error C2039: “addTextureMesh”: 不是“pcl::visualization::PCLVisualizer”的成员
error C2039: "addTextureMesh": 不是"pcl::visualization::PCLVisualizer"的成员 PCL 1.6 ...