Python 通过print将数据保存到文件中
1. Print them to screen
man = []
other = []
try:
data = open('sketch.txt')
for each_line in data:
try:
(role, line_spoken) = each_line.split(':',1)
line_spoken=line_spoken.strip()
if role== 'Man':
man.append(line_spoken)
elif role == 'Other Man':
other.append(line_spoken)
except ValueError:
pass
data.close()
except IOError:
print ('The datafiel is missing!') print('printing=====man===list=======',man)
print('printing=====other===list=====',other)
2. Save the data to man_data.txt and other_data.txt
man = []
other = []
try:
data = open('sketch.txt')
for each_line in data:
try:
(role, line_spoken) = each_line.split(':',1)
line_spoken=line_spoken.strip()
if role== 'Man':
man.append(line_spoken)
elif role == 'Other Man':
other.append(line_spoken)
except ValueError:
pass
data.close()
except IOError:
print ('The datafiel is missing!') try:
man_out = open("man_data.txt","w")
other_out = open('other_data.txt','a') print('here is data from list',man,file=man_out)
print('save man file to man_data.txt')
print('here is data from list',other,file=other_out)
print('save other file to other_data.txt') man_out.close()
other_out.close()
except IOError:
print ('data file is not exist')
Python 通过print将数据保存到文件中的更多相关文章
- Python 通过print_lol将数据保存到文件中
1. 定义一个print_lol函数来控制列表的缩进和写入位置 import sys """this is a new fuction, which work for a ...
- python将字典中的数据保存到文件中
d = {'a':'aaa','b':'bbb'}s = str(d)f = open('dict.txt','w')f.writelines(s)f.close()
- Python3操作MySQL,查询数据并保存到文件中
我们在测试过程中,可能需要到数据库中拉去一些数据,为从测试准备.比如最近在做接口性能测试的时候,就需要很多数据来支撑,所以就需要的数据库去查询数据,下面就是python3 查询 mysql 并且保存到 ...
- 『无为则无心』Python日志 — 66、将日志信息保存到文件中
目录 1.把日志信息保存到文件中 2.拓展 (1)观察代码 (2)提出问题 (3)问题说明 1.把日志信息保存到文件中 代码如下所示: """ logging模块是Pyt ...
- php cUrl模拟登录,cookie保存到文件中
源码如下: <?php header("Content-Type:text/html;charset=utf-8"); //模拟群友通讯录手机号登录 $curl = curl ...
- json和xml封装数据、数据缓存到文件中
一.APP的通信格式之xml xml:扩展标记语言,可以用来标记数据,定义数据类型,是一种允许用户对自己标记语言进行定义的源语言.XML格式统一,扩平台语言,非常适合数据传输和通信,业界公认的标准. ...
- 利用FFmpeg 将 rtsp 获取H264裸流并保存到文件中
既然已经可以通过 RTSP 获取h264 裸流了.那么通过 FFmpeg 将其保存到文件中怎么做呢? 一.首先RTSP获取 h264 裸流 我们上面两篇文章主要讲的是通过 rtsp://Your ip ...
- Object 保存到文件中
6月4日 Object 保存到文件中 Q. 你添加一个新类到你的项目当中且你希望可以保存这个类的一个实例对象到磁盘文件 并在需要时从磁盘文件读回到内存中 A. 方案 确保你的类遵循 NSCodi ...
- Java将对象保存到文件中/从文件中读取对象
1.保存对象到文件中 Java语言只能将实现了Serializable接口的类的对象保存到文件中,利用如下方法即可: public static void writeObjectToFile(Obje ...
随机推荐
- archlinux设置无线网络连接
Check the driver $ lspci -k Open the network card $ ip link set <设备名> up Check device status $ ...
- iOS学习笔记---c语言第八天
指针 首先将变量a的地址存放在另一个变量中,比如存放在变量b中,然后通过变量b来间接引用变量a,间接读写变量a的值.用来存放变量地址的变量,就称为"指针变量" int *p=nul ...
- [转载] C++ string, const char*, char* 之间互相转换
1, string转const char* 类型 string str = "abcdef"; const char* con_str = string.c_str() 2, co ...
- jce
jdk8:jce 下载地址:http://www.oracle.com/technetwork/java/javase/downloads/jce8-download-2133166.html jdk ...
- Android——BaseAdapter相关
layout文件: <?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:an ...
- POJ-2528 Mayor's posters (线段树区间更新+离散化)
题目分析:线段树区间更新+离散化 代码如下: # include<iostream> # include<cstdio> # include<queue> # in ...
- hdu1285 拓扑序
题意:有N个比赛队(1<=N<=500),编号依次为1,2,3,....,N进行比赛,比赛结束后,裁判委员会要将所有参赛队伍从前往后依次排名,但现在裁判委员会不能直接获得每个队的比赛成绩, ...
- 锁_rac环境kill锁表会话后出现killed状态(解决)
原创作品,出自 "深蓝的blog" 博客,深蓝的blog:http://blog.csdn.net/huangyanlong/article/details/46876961 ra ...
- LINQ及EntityFramework何时从数据库返回数据,备忘
Generally speaking, LINQ queries are executed when the application code processes data (for instance ...
- Java基础试题
1.使用Java语言编写的源程序保存时的文件扩展名是( B ). (A).class (B).java (C).cpp (D).txt ...