在HeadFirstPython网站中下载所有文件,解压后以chapter 3中的“sketch.txt”为例:
新建IDLE会话,首先导入os模块,并将工作目录却换到包含文件“sketch.txt”的文件夹,如C:\\Python33\\HeadFirstPython\\chapter3
复制代码 代码如下:
> import os
> os.getcwd() #查看当前工作目录
'C:\\Python33'
> os.chdir('C:/Python33/HeadFirstPython/chapter3') #切换包含数据文件的文件夹
> os.getcwd() #查看切换后的工作目录
'C:\\Python33\\HeadFirstPython\\chapter3'
打开文件“sketch.txt”,读取并显示前两行:
复制代码 代码如下:
> data=open('sketch.txt')
> print(data.readline(),end='')
Man: Is this the right room for an argument?
> print(data.readline(),end='')
Other Man: I've told you once.
回到文件起始位置,使用for语句处理文件中的每行,最后关闭文件:
复制代码 代码如下:
> data.seek(0) #使用seek()方法回到文件起始位置
> for each_line in data:
print(each_line,end='')
Man: Is this the right room for an 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 and paying) Just the five minutes.
Other Man: Just the five minutes. Thank you.
Other Man: Anyway, I did.
Man: You most certainly did not!
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 it is!
Man: No it isn't!
(pause)
Man: It's just contradiction!
Other Man: No it isn't!
Man: It IS!
Other Man: It is NOT!
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, this is futile!!
(pause)
Other Man: No it isn't!
Man: Yes it is!
> data.close()
读取文件后,将不同role对应数据分别保存到列表man和other:
复制代码 代码如下:
import os
print(os.getcwd())
os.chdir('C:\Python33\HeadFirstPython\chapter3')
man=[] #定义列表man接收Man的内容
other=[] #定义列表other接收Other Man的内容
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 datafile is missing!')
print (man)
print (other)
Tips:
使用open()方法打开磁盘文件时,默认的访问模式为r,表示读,不需要特意指定;
要打开一个文件完成写,需要指定模式w,如data=open("sketch.txt","w"),如果该文件已经存在则会清空现有内容;
要追加到一个文件,需要指定模式a,不会清空现有内容;
要打开一个文件完成写和读,且不清空现有内容,需要指定模式w+;
例如,将上例中保存的man和other内容以文件方式保存时,可修改如下:
复制代码 代码如下:
import os
print(os.getcwd())
os.chdir('C:\Python33\HeadFirstPython\chapter3')
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 datafile is missing!')
try:
man_file=open('man.txt', 'w') #以w模式访问文件man.txt
other_file=open('other.txt','w') #以w模式访问文件other.txt
print (man, file=man_file) #将列表man的内容写到文件中
print (other, file=other_file)
except IOError:
print ('File error')
finally:
man_file.close()
other_file.close()
但是第26行print()为什么会报错?“syntax error while detecting tuple”,有大神能给解惑一下不
免责声明:本站资源来自互联网收集,仅供用于学习和交流,请遵循相关法律法规,本站一切资源不代表本站立场,如有侵权、后门、不妥请联系本站删除!
P70系列延期,华为新旗舰将在下月发布
3月20日消息,近期博主@数码闲聊站 透露,原定三月份发布的华为新旗舰P70系列延期发布,预计4月份上市。
而博主@定焦数码 爆料,华为的P70系列在定位上已经超过了Mate60,成为了重要的旗舰系列之一。它肩负着重返影像领域顶尖的使命。那么这次P70会带来哪些令人惊艳的创新呢?
根据目前爆料的消息来看,华为P70系列将推出三个版本,其中P70和P70 Pro采用了三角形的摄像头模组设计,而P70 Art则采用了与上一代P60 Art相似的不规则形状设计。这样的外观是否好看见仁见智,但辨识度绝对拉满。
更新日志
- 《原神》「星路拾忆」网页活动壁纸展示
- 群星.2024-祝你幸福!电影原声带【阿里巴巴】【FLAC分轨】
- 于台烟.2004-执着【乾坤唱片】【WAV+CUE】
- 黄品源.2004-感谢·情人(引进版)【滚石】【WAV+CUE】
- 《最终幻想16》评测:游、戏两难全的系列转折
- 《纸嫁衣》五部打包¥104,《鬼泣5》仅需¥44
- 《崩坏:星穹铁道》造物大赛卷出新高度
- 《王菲 菲常传奇 2CD》[WAV+CUE][950MB]
- 《王菲 空灵独味唱腔 非常传奇 紫水晶2CD》[WAV+CUE][860MB]
- 《车载专业测试王1号 极品音质》[WAV/分轨][980MB]
- 炉石传说的国服回归送什么奖励 炉石传说9.25回归最新消息介绍
- 云顶之弈最强阵容汇总2024 云顶之弈最强阵容搭配最新版本
- S14lol全球总决赛在哪观看 lol全球总决赛观赛直播地址汇总
- 黄思婷2011-感谢天[豪记唱片][WAV+CUE]
- 孙淑媚2013-太阳[台湾首版][WAV+CUE]