Python编程:从入门到实践 第10章

275 阅读8分钟

Python编程:从入门到实践 第10章课后习题,前几天过节去了,囧。

10-1 Python学习笔记:在文本编辑器中新建一个文件,写几句话来总结一下你至此学到的Python知识,其中每一行都以“In Python you can”打头。将这个文件命名为learning_python.txt,并将其存储到为完成本章练习而编写的程序所在的目录中。编写一个程序,它读取这个文件,并将你所写的内容打印三次:第一次打印时读取整个文件;第二次打印时遍历文件对象;第三次打印时将各行存储在一个列表中,再在with代码块外打印它们。

file_name = 'learning_python.txt'

with open(file_name, encoding='UTF-8') as file_object:
    content = file_object.read()
    print(content)

with open(file_name, encoding='UTF-8') as file_object:
    for lines in file_object:
        print(lines.rstrip())

with open(file_name, encoding='UTF-8') as file_object:
    lines = file_object.readlines()

for line in lines:
    print(line.rstrip())

因为有中文,不加encoding = utf-8的话报错:

UnicodeDecodeError: 'gbk' codec can't decode byte 0x82 in position 85: illegal multibyte sequence

结果如下:

In Python you can type.
In Python you can do statistics.
In Python you can 学习。
In Python you can 工作。

In Python you can type.
In Python you can do statistics.
In Python you can 学习。
In Python you can 工作。
In Python you can type.
In Python you can do statistics.
In Python you can 学习。
In Python you can 工作。

10-2 C语言学习笔记:可使用方法replace()将字符串中的特定单词都替换为另一个单词。下面是一个简单的示例,演示了如何将句子中的'dog' 替换为'cat' :

>>> message = "I really like dogs." 
>>> message.replace('dog', 'cat') 
'I really like cats.' 

读取你刚创建的文件learning_python.txt中的每一行,将其中的Python都替换为另一门语言的名称,如C。将修改后的各行都打印到屏幕上。

def content_lower(c):
    return [content.lower() for content in c]


file_name = 'learning_python.txt'

with open(file_name, encoding='utf-8') as file_object:
    contents = file_object.readlines()

contents = content_lower(contents)

for line in contents:
    print(line.replace('python', 'Ruby').rstrip())

replace不能分辨大小写,因此,如果replace里面是小写p的python,则无法替代。当然可以在replace中直接用大写,但我想把原文件中的大写弄成小写。正好这时候复习了一下函数。最后一句反过来先rstrip也一样的效果。

10-3 访客:编写一个程序,提示用户输入其名字;用户作出响应后,将其名字写入到文件guest.txt中。

file_name = 'guest.txt'

with open(file_name, 'w') as file_object:
    user_name = input('Username please:\n\t')
    file_object.write(user_name+'\n')

10-4 访客名单:编写一个while 循环,提示用户输入其名字。用户输入其名字后,在屏幕上打印一句问候语,并将一条访问记录添加到文件guest_book.txt中。确保这个文件中的每条记录都独占一行。

file_name = 'guest.txt'

print('Enter quit when you finish.')

while True:
    name = input("What's your name?")
    if name == 'quit':
        break
    else:
        with open(file_name,'a') as file_object:
            file_object.write(name+'\n')
            print('Hello,', name + '.')

这个方法写出来的txt文件时ANSI编码,查了一下百度,如果需要utf-8则需要导入codecs包,然后with codecs.open()

import codecs

file_name = 'guest.txt'

print('Enter quit when you finish.')

while True:
    name = input("What's your name?")
    if name == 'quit':
        break
    else:
        with codecs.open(file_name,'a', 'utf-8') as file_object:
            file_object.write(name+'\n')
            print('Hello,', name + '.')

10-5 关于编程的调查:编写一个while 循环,询问用户为何喜欢编程。每当用户输入一个原因后,都将其添加到一个存储所有原因的文件中。

可以按照上面的重写一次,但是看了eric matthew的示范,他通过列表写的,姑且放在这里:

file_name = 'reason.txt'

answers = []

while True:
    answer = input('Why do you like coding?')
    answers.append(answer)

    continue_poll = input('Would you like to continue? y/n')
    if continue_poll != 'y':
        break

with open(file_name, 'a') as file_object:
    for answer in answers:
        file_object.write(answer+'\n')

10-6 加法运算:提示用户提供数值输入时,常出现的一个问题是,用户提供的是文本而不是数字。在这种情况下,当你尝试将输入转换为整数时,将引发TypeError异常。编写一个程序,提示用户输入两个数字,再将它们相加并打印结果。在用户输入的任何一个值不是数字时都捕获TypeError异常,并打印一条友好的错误消息。对你编写的程序进行测试:先输入两个数字,再输入一些文本而不是数字。

num1 = input('Enter number 1:')
num2 = input('Enter number 2:')

try:
    answer = int(num1) + int(num2)
except ValueError:
    print('Digits please.')
else:
    print('The answer is :', str(answer) + '.')

10-7 加法计算器:将你为完成练习10-6而编写的代码放在一个while 循环中,让用户犯错(输入的是文本而不是数字)后能够继续输入数字。

print('Enter q to quit.')

while True:
    try:
        num1 = input('Number 1:')
        if num1 == 'q':
            break
        else:
            num1 = int(num1)

        num2 = input('Number 2:')
        if num2 == 'q':
            break
        else:
            num2 = int(num2)

    except ValueError:
        print('Digits please.')

    else:
        answer = num1 + num2
        print('The answer is', str(answer) + '.')

看了eric的代码、外加重新看书后,发现书里这一句:

依赖于try 代码块成功执行的代码都应放到else 代码块中

所以把answer放到else里面去了(之前是放到try里面的)。

10-8 猫和狗:创建两个文件cats.txt和dogs.txt,在第一个文件中至少存储三只猫的名字,在第二个文件中至少存储三条狗的名字。编写一个程序,尝试读取这些文件,并将其内容打印到屏幕上。将这些代码放在一个try-except 代码块中,以便在文件不存在时捕获FileNotFound 错误,并打印一条友好的消息。将其中一个文件移到另一个地方,并确认except 代码块中的代码将正确地执行。

file_Cats = 'cats.txt'
file_Dogs = 'dogs.txt'

with open(file_Cats, 'a') as object_cats:
    object_cats.write('siamese\n')
    object_cats.write('persian\n')
    object_cats.write('british shorthair\n')

with open(file_Dogs, 'a') as object_dogs:
    dog_list = ['corgy', 'golden', 'Labrador']
    for x in dog_list:
        object_dogs.write(x+'\n')

先创建两个txt文件。

cats_list = 'cats.txt'
dogs_list = 'dogs.txt'

try:
    with open(cats_list) as c_l:
        content_cats = c_l.readlines()
        print(content_cats)
except FileNotFoundError:
    print('No list found.')

try:
    with open(dogs_list) as c_l:
        content_cats = c_l.readlines()
        print(content_cats)
except FileNotFoundError:
    print('No list found.')

这个简单:

files = ['cats.txt', 'dogs.txt']
for x in files:
    try:
        with open(x) as obj:
            content = obj.readlines()
            print(content)
    except FileNotFoundError:
        print('No list found.')

10-9 沉默的猫和狗:修改你在练习10-8中编写的except代码块,让程序在文件不存在时一言不发。

把dogs.txt改成了dog.txt:

files = ['cats.txt', 'dog.txt']
for x in files:
    try:
        with open(x) as obj:
            content = obj.readlines()
            print(content)
    except FileNotFoundError:
        pass

10-10 常见单词:访问项目Gutenberg(gutenberg.org/ ),并找一些你想分析的图书。下载这些作品的文本文件或将浏览器中的原始文本复制到文本文件中。

你可以使用方法count() 来确定特定的单词或短语在字符串中出现了多少次。例如,下面的代码计算'row' 在一个字符串中出现了多少次:

>>> line = "Row, row, row your boat" 
>>> line.count('row') 
2 
>>> line.lower().count('row') 
3

请注意,通过使用lower() 将字符串转换为小写,可捕捉要查找的单词出现的所有次数,而不管其大小写格式如何。编写一个程序,它读取你在项目Gutenberg中获取的文件,并计算单词'the' 在每个文件中分别出现了多少次。

import codecs


def words_count(file_name, word):
    """counting words number"""
    try:
        with codecs.open(file_name, 'r', 'utf-8') as file_obj:
            content = file_obj.read()
    except FileNotFoundError:
        print(file_name, "doesn't found.")
    else:
        num = content.lower().count(word)
        print(f"\nThe word of '{word}' appears in {file_name} for about {num} times.")


print('Enter q to quit at anytime.')
while True:
    try:
        book_name = input('\nEnter book name to be analyzed:')+'.txt'
        if book_name == 'q':
            break

        word_name = input('Enter the word to be analyzed:')
        if word_name == 'q':
            break

    except FileNotFoundError:
        print('No file')
    else:
        result = words_count(book_name, word_name)

结果:

Enter q to quit at anytime.

Enter book name to be analyzed:The Adventures of Sherlock Holmes by Arthur Conan Doyle
Enter the word to be analyzed:the

The word of 'the' appears in The Adventures of Sherlock Holmes by Arthur Conan Doyle.txt for about 8001 times.

Enter book name to be analyzed:alice
Enter the word to be analyzed:the
alice.txt doesn't found.

Enter book name to be analyzed:q
Enter the word to be analyzed:q

但这么写,最后的调用函数的那一行弄不好就报错:运行之后,我本想复制图书名字,结果忘记之前ctrl+c的是代码于是代码直接复制了进去结果报错。

10-11 喜欢的数字:编写一个程序,提示用户输入他喜欢的数字,并使用json.dump() 将这个数字存储到文件中。再编写一个程序,从文件中读取这个值,并打印 消息“I know your favorite number! It's _____.”。

import json

fav_num = input('Enter a number:')
file_name = 'fav_num.json'

with open(file_name, 'w') as obj:
    json.dump(fav_num,obj)

with open(file_name) as f:
    number = json.load(f)
    print('I know your favorite number! It's', str(number)+'.')

10-12 记住喜欢的数字:将练习10-11中的两个程序合而为一。如果存储了用户喜欢的数字,就向用户显示它,否则提示用户输入他喜欢的数字并将其存储到文件中。运行这个程序两次,看看它是否像预期的那样工作。

import json

file_name = 'fav_num.json'
try:
    with open(file_name) as obj:
        num = json.load(obj)
except FileNotFoundError:
    num = input('Enter a favorite number:')
    with open(file_name,'w') as obj:
        json.dump(num, obj)
        print('I will remember that.')
else:
    print('I know your favorite number is:', num)

10-13 验证用户:最后一个remember_me.py版本假设用户要么已输入其用户名,要么是首次运行该程序。我们应修改这个程序,以应对这样的情形:当前和最后一次运行该程序的用户并非同一个人。

为此,在greet_user() 中打印欢迎用户回来的消息前,先询问他用户名是否是对的。如果不对,就调用get_new_username() 让用户输入正确的用户名。

import json


def get_stored_username():
    file_name = "user.json"
    try:
        with open(file_name) as obj:
            username = json.load(obj)
    except FileNotFoundError:
        return None
    else:
        return username


def get_new_username():
    username = input("What's your name?")
    file_name = "user.json"
    with open(file_name, 'w') as obj:
        json.dump(username, obj)
    return username


def greet_user():
    username = get_stored_username()
    if username:
        option = input("Is this your username? " + username + "\ny or n?")
        if option == 'y':
            print("Welcome back, " + username)
        else:
            username = get_new_username()
            print("I will remember that," + username)
    else:
        username = get_new_username()
        print("I will remember that," + username)


greet_user()

最后的def greet_user():刚运行起来一直报错,似乎是提示传入def greet_user():的argument的数量不对,后来发现是不少语句里面用,代替空格和加号造成的。看来这个习惯要改。