笔记 第7章 文件

阅读的书籍是: Python for Informatics: Exploring Information   网址: http://www.pythonlearn.com/book.php

编写的代码一:

1 fhand = open('mbox-short.txt')
2 for line in fhand:   ## for 每次读入一行数据
3     if line.startswith('From:'):   ##使用字符串startwith方法来选择符合前缀要求的行
4         print line

输出效果为:

From: stephen.marquard@uct.ac.za

From: louis@media.berkeley.edu

From: zqian@umich.edu

...

编写的代码二:

1 fhand = open('mbox-short.txt')
2 for line in fhand:
3     line = line.rstrip()   ##使用rstrip方法截掉字符串后面的空白符
4     if line.startswith('From:'):  
5         print line

输出效果为:

 

From: stephen.marquard@uct.ac.za
From: louis@media.berkeley.edu
From: zqian@umich.edu
From: rjlowe@iupui.ed
...

 

习题7.1 编写一个程序,读取一个文件,以大写方式逐行打印出文件内容。程序运行结果如下所示:

python shout.py
Enter a file name: mbox-short.txt
FROM STEPHEN.MARQUARD@UCT.AC.ZA SAT JAN  5 09:14:16 2008
RETURN-PATH: <POSTMASTER@COLLAB.SAKAIPROJECT.ORG>
RECEIVED: FROM MURDER (MAIL.UMICH.EDU [141.211.14.90])
  BY FRANKENSTEIN.MAIL.UMICH.EDU (CYRUS V2.3.8) WITH LMTPA;
  SAT, 05 JAN 2008 09:14:16 -0500
try:  
    fhand = open(raw_input('Enter a file name:'))
except:
    print 'open file err'
    exit()
    
count = 0
for line in fhand:
    count = count + 1
    if count <=5:
        line = line.rstrip().upper()
        print line
    else:
        break 
View Code

 

习题7.2 编写一个程序,让用户输入文件名,然后读取文件,按行的形式j进行查看。

X-DSPAM-Confidence: 0.8475

遇到以“X-DSPAM-Confidence:”开头的行,提取该行中的浮点数。统计行数,计算这些行的垃圾邮件信度值。文件读取结束后,打印垃圾邮件平均信度。

Enter the file name: mbox.txt
Average spam confidence: 0.894128046745

Enter the file name: mbox-short.txt
Average spam confidence: 0.750718518519
try:
    fhand = open(raw_input('Enter the file name:'))
except:
    print 'file open err'
    exit()

flinecnt = 0;
count = 0
for line in fhand:
    flinecnt = flinecnt + 1;
    if line.startswith('X-DSPAM-Confidence:'):
        count = count + 1
    else:
        continue

print float(count)/float(flinecnt)
print count
View Code debug

 

try:
    fhand = open(raw_input('Enter the file name:'))
except:
    print 'file open err'
    exit()

sum = 0;
count = 0
for line in fhand:
    if line.startswith('X-DSPAM-Confidence:'):
        count = count + 1
        sum = sum + float(line.split(':')[1].lstrip())  ##???
    else:
        continue

print "average spam confidence:", sum/count

 

posted @ 2016-03-02 20:02  .Think  阅读(452)  评论(0编辑  收藏  举报