python的head函数输不出数据_HeadFirstPython学习_从文件读取数据和保存数据

运用Python中的内置函数open()与文件进行交互

在HeadFirstPython网站中下载所有文件,解压后以chapter 3中的“sketch.txt”为例:

721a5bf7eb2e67c2e00704f81bb5cf6f.png

新建IDLE会话,首先导入os模块,并将工作目录却换到包含文件“sketch.txt”的文件夹,如C:\\Python33\\HeadFirstPython\\chapter3

fcecaa27ea5212ceb9bf034c36bfbf34.gif>>> importos>>> os.getcwd() #查看当前工作目录

‘C:\\Python33‘

>>> os.chdir(‘C:/Python33/HeadFirstPython/chapter3‘) #切换包含数据文件的文件夹

>>> os.getcwd() #查看切换后的工作目录

‘C:\\Python33\\HeadFirstPython\\chapter3‘

fcecaa27ea5212ceb9bf034c36bfbf34.gif

打开文件“sketch.txt”,读取并显示前两行:

fcecaa27ea5212ceb9bf034c36bfbf34.gif>>> data=open(‘sketch.txt‘)>>> print(data.readline(),end=‘‘)

Man: Is this the right roomforan argument?>>> print(data.readline(),end=‘‘)

Other Man: I‘ve told you once.

fcecaa27ea5212ceb9bf034c36bfbf34.gif

回到文件起始位置,使用for语句处理文件中的每行,最后关闭文件:

2b65ef29a5872cc0e4771c25889edd04.gif

6a087676c59fa8b19d76e6bb55a32902.gif

>>> data.seek(0) #使用seek()方法回到文件起始位置

0>>> for each_line indata:print(each_line,end=‘‘)

Man: Is this the right roomforan argument?

Other Man: I‘ve told you once.

Man: No you haven‘t!

Other Man: Yes I have.

Man: When?

Other Man: Just now.

Man: No you didn‘t!

Other Man: Yes I did!

Man: You didn‘t!

Other Man: I‘m telling you, I did!

Man: You did not!

Other Man: Oh I‘m sorry, is this a five minute argument, or the full half hour?

Man: Ah! (taking out his wallet andpaying) Just the five minutes.

Other Man: Just the five minutes. Thank you.

Other Man: Anyway, I did.

Man: You most certainly didnot!

Other Man: Now let‘s get one thing quite clear: I most definitely told you!

Man: Oh no you didn‘t!

Other Man: Oh yes I did!

Man: Oh no you didn‘t!

Other Man: Oh yes I did!

Man: Oh look, this isn‘t an argument!

(pause)

Other Man: Yes itis!

Man: No it isn‘t!

(pause)

Man: It‘s just contradiction!

Other Man: No it isn‘t!

Man: It IS!

Other Man: ItisNOT!

Man: You just contradicted me!

Other Man: No I didn‘t!

Man: You DID!

Other Man: No no no!

Man: You did just then!

Other Man: Nonsense!

Man: (exasperated) Oh, thisisfutile!!

(pause)

Other Man: No it isn‘t!

Man: Yes it is!>>> data.close()View Code

读取文件后,将不同role对应数据分别保存到列表man和other:

fcecaa27ea5212ceb9bf034c36bfbf34.gifimportosprint(os.getcwd())

os.chdir(‘C:\Python33\HeadFirstPython\chapter3‘)

man=[] #定义列表man接收Man的内容

other=[] #定义列表other接收Other Man的内容

try:

data=open("sketch.txt")for each_line indata: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)exceptValueError:passdata.close()exceptIOError:print(‘The datafile is missing!‘)print(man)print (other)

fcecaa27ea5212ceb9bf034c36bfbf34.gif

Tips:

使用open()方法打开磁盘文件时,默认的访问模式为r,表示读,不需要特意指定;

要打开一个文件完成写,需要指定模式w,如data=open("sketch.txt","w"),如果该文件已经存在则会清空现有内容;

要追加到一个文件,需要指定模式a,不会清空现有内容;

要打开一个文件完成写和读,且不清空现有内容,需要指定模式w+;

例如,将上例中保存的man和other内容以文件方式保存时,可修改如下:

fcecaa27ea5212ceb9bf034c36bfbf34.gif1 importos2 print(os.getcwd())3 os.chdir(‘C:\Python33\HeadFirstPython\chapter3‘)4 man=[]5 other=[]6

7 try:8 data=open("sketch.txt")9 for each_line indata:10 try:11 (role, line_spoken)=each_line.split(‘:‘, 1)12 line_spoken=line_spoken.strip()13 if role==‘Man‘:14 man.append(line_spoken)15 elif role==‘Other Man‘:16 other.append(line_spoken)17 exceptValueError:18 pass

19 data.close()20 exceptIOError:21 print(‘The datafile is missing!‘)22

23 try:24 man_file=open(‘man.txt‘, ‘w‘) #以w模式访问文件man.txt

25 other_file=open(‘other.txt‘,‘w‘) #以w模式访问文件other.txt

26 print (man, file=man_file) #将列表man的内容写到文件中

27 print (other, file=other_file)28 exceptIOError:29 print (‘File error‘)30 finally:31 man_file.close()32 other_file.close()

fcecaa27ea5212ceb9bf034c36bfbf34.gif

但是第26行print()为什么会报错?“syntax error while detecting tuple”

原文:http://www.cnblogs.com/liutong3310/p/3727916.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值