从文件中读取数据:文件分析-python进阶篇十二

  下面来提取童话 Alicein Wonderland 的文本,并尝试计算它包含多少个单词。我们将使用方法split() ,它根据一个字符串创建一个单词列表。方法split() 以空格为分隔符将字符串分拆成多个部分,并将这些部分都存储到一个列表中。

>>> title = "Alice in Wonderland"
>>> title.split()
[Alice, in, Wonderland]
filename = alice.txt
try:
    with open(filename) as f_obj:
        contents = f_obj.read()
except FileNotFoundError:
    msg = "Sorry, the file " + filename + " does not exist."
    print(msg)
else:
    # 计算文件大致包含多少个单词
    words = contents.split()
    num_words = len(words)
    print("The file " + filename + " has about " + str(num_words) + " words.")

#The file alice.txt has about 29461 words.

  分析多个文件

def count_words(filename):
    """计算一个文件大致包含多少个单词"""
    try:
        with open(filename) as f_obj:
        contents = f_obj.read()
    except FileNotFoundError:
        msg = "Sorry, the file " + filename + " does not exist."
        print(msg)
    else:
        # 计算文件大致包含多少个单词
        words = contents.split()
        num_words = len(words)
        print("The file " + filename + " has about " + str(num_words) +" words.")

filenames = [alice.txt, siddhartha.txt, moby_dick.txt, little_women.txt]
for filename in filenames:
    count_words(filename) #循环执行函数

‘‘‘
The file alice.txt has about 29461 words.
Sorry, the file siddhartha.txt does not exist.有一个文件没有
The file moby_dick.txt has about 215136 words.
The file little_women.txt has about 189079 words.
‘‘‘

  如果缺少一个文件但是不想报错,就像什么都没有发生一样继续运行。个pass语句,可在代码块中使用它来让Python 什么都不要做。

def count_words(filename):
    """计算一个文件大致包含多少个单词"""
try:
    --snip--
except FileNotFoundError:
    pass #直接过,什么都不做
else:
    --snip--

filenames = [alice.txt, siddhartha.txt, moby_dick.txt, little_women.txt]
for filename in filenames:
    count_words(filename)
‘‘‘
The file alice.txt has about 29461 words.
The file moby_dick.txt has about 215136 words.
The file little_women.txt has about 189079 words.
‘‘‘

 

从文件中读取数据:文件分析-python进阶篇十二

上一篇:用Windows操作系统的人有时会遇到这样的错误信息:


下一篇:Java面向对象的概念和定义