以写模式打开文件:需要指定写模式,如下所示

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学习三(数据保存到文件)的更多相关文章

  1. Python 脚本生成测试数据,Python生成随机数据,Python生成大量数据保存到文件夹中

    代码如下: import random import datetime import time dataCount = 10*100*100 #10M. codeRange = range(ord(' ...

  2. [Python] Python 学习 - 可视化数据操作(一)

    Python 学习 - 可视化数据操作(一) GitHub:https://github.com/liqingwen2015/my_data_view 目录 折线图 散点图 随机漫步 骰子点数概率 文 ...

  3. python将控制台输出保存到文件

    python将控制台输出保存到文件   在平时工作中,有时我们需要将控制台输出保存到文件 1.命令行用>覆盖写入和>>追加写入 for i in range(10000): prin ...

  4. Python学习笔记_Chapter 4数据保存到文件

    1. What For 将基于内存的数据存储到磁盘上,达到持续存储. 2. HOW 方法一: 将数据写到文件中 常规的处理方式 #file.x被打开的文件,model打开文件的方式 out=open( ...

  5. Python 通过print_lol将数据保存到文件中

    1. 定义一个print_lol函数来控制列表的缩进和写入位置 import sys """this is a new fuction, which work for a ...

  6. Python 通过print将数据保存到文件中

    1. Print them to screen man = [] other = [] try: data = open('sketch.txt') for each_line in data: tr ...

  7. python将字典中的数据保存到文件中

    d = {'a':'aaa','b':'bbb'}s = str(d)f = open('dict.txt','w')f.writelines(s)f.close()

  8. Python学习记录----数据定义

    摘要: 描述Python中数据定义格式,需要注意的东东. 一 数据声明 Python木有一般语言的具体数据类型,像char,int,string这些通通木有.这有点像javascript,但又不同,j ...

  9. Python scrapy爬虫数据保存到MySQL数据库

    除将爬取到的信息写入文件中之外,程序也可通过修改 Pipeline 文件将数据保存到数据库中.为了使用数据库来保存爬取到的信息,在 MySQL 的 python 数据库中执行如下 SQL 语句来创建 ...

随机推荐

  1. Poj1218_THE DRUNK JAILER(水题)

    一.Description A certain prison contains a long hall of n cells, each right next to each other. Each ...

  2. 串口编程3:使用串口读取GPS信息

    关于GPS的使用,参考. 本文主要参考的博客,在此表示感谢!!! 主函数 主函数gps_main.c,这里便涉及到了串口的打开,读操作,以及调用了串口设置函数: #include <stdio. ...

  3. n文件的上传和下载,struts2和springmvc

    首先,struts2的上传下载的配置 因为struts2是配置的上传的拦截器,很简单的步揍就可以上传, 首先是配置struts的action映射 <!-- 4. 修改上传文件的最大大小为30M ...

  4. VisualGDB系列1:VisualGDB总体概述

    根据VisualGDB官网(https://visualgdb.com)的帮助文档大致翻译而成.主要是作为个人学习记录.有错误的地方,Robin欢迎大家指正. 本文总体介绍VisualGDB能给你带来 ...

  5. python 基础 列表 增删改查

    names = ["aaron", "alex", "james", "meihengfan"]names2 = [1, ...

  6. SpringSecurity02 表单登录、SpringSecurity配置类

    1 功能需求 springSecuriy默认的登录窗口是一个弹出窗口,而且会默认对所有的请求都进行拦截:要求更改登录页面(使用表单登录).排除掉一些请求的拦截 2 编写一个springSecurity ...

  7. 在64位ubuntu上安装alienbrain客户端

    一.首先从Alienbrain_EN_10.5.zip安装包(网上可搜索下载)里提取出linux版安装文件:Installations/Clients/Linux/NoVM/install.bin并c ...

  8. ibatis分页的两种方式

    1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 3 ...

  9. Eclipse提交svn错误svn E210003 connection refused by the server

    错误明细: org.apache.subversion.javahl.ClientException: svn: E210003: connection refused by the server o ...

  10. 树莓派配置(一):打开SPI

    1.树莓派默认SPI关闭,在进行编程前需要打开SPI cd /boot/ sudo vi config.txt 将#dtparam=spi=off 改成:dtparam=spi=on 重启 sudo ...