with open("learning_python.txt") as f: print(f.read()) with open("learning_python.txt") as f: for line in f: print(line, end = '') with open("learning_python.txt") as f: all_lines = f.readlines() for l in all_lines: print(l, end = '')
learning_python.txt
In python you can 使用string对象 In python you can 用for或while语法进行循环 In python you can 调用函数 In python you can 自定义类
输出:
In python you can 使用string对象 In python you can 用for或while语法进行循环 In python you can 调用函数 In python you can 自定义类 In python you can 使用string对象 In python you can 用for或while语法进行循环 In python you can 调用函数 In python you can 自定义类In python you can 使用string对象 In python you can 用for或while语法进行循环 In python you can 调用函数 In python you can 自定义类
10-2 C语言学习笔记 :可使用方法replace() 将字符串中的特定单词都替换为另一个单词。
读取你刚创建的文件learning_python.txt中的每一行,将其中的Python都替换为另一门语言的名称,如C。将修改后的各行都打印到屏幕上。
with open("learning_python.txt") as f: print(f.read().replace('python', 'Java'))输出:
In Java you can 使用string对象 In Java you can 用for或while语法进行循环 In Java you can 调用函数 In Java you can 自定义类
10-4 访客名单:编写一个while 循环,提示用户输入其名字。用户输入其名字后,在屏幕上打印一句问候语,并将一条访问记录添加到文件guest_book.txt中。确保这 个文件中的每条记录都独占一行。
from time import * with open('guest_book.txt', 'w') as gb: while True: name = input('请输入您的名字,以"q"结束:') if name == 'q': break print('欢迎回来, ' + name) gb.write('%-10s%s\n' % (name, asctime(localtime(time()))))输出:
请输入您的名字,以"q"结束:Amy 欢迎回来, Amy 请输入您的名字,以"q"结束:Lucia 欢迎回来, Lucia 请输入您的名字,以"q"结束:Charlotte 欢迎回来, Charlotte 请输入您的名字,以"q"结束:Sarah 欢迎回来, Sarah 请输入您的名字,以"q"结束:Tommy 欢迎回来, Tommy 请输入您的名字,以"q"结束:q PS D:\Resource\Code\python>guest_book.txt
Amy Mon Apr 2 09:31:39 2018 Lucia Mon Apr 2 09:31:45 2018 Charlotte Mon Apr 2 09:31:52 2018 Sarah Mon Apr 2 09:32:19 2018 Tommy Mon Apr 2 09:33:02 2018
10-6 加法运算:提示用户提供数值输入时,常出现的一个问题是,用户提供的是文本而不是数字。在这种情况下,当你尝试将输入转换为整数时,将引 发TypeError 异常。编写一个程序,提示用户输入两个数字,再将它们相加并打印结果。在用户输入的任何一个值不是数字时都捕获TypeError 异常,并打印一条友好的错误消息。对你编写的程序进行测试:先输入两个数字,再输入一些文本而不是数字。
10-7 加法计算器:将你为完成练习10-6而编写的代码放在一个while 循环中,让用户犯错(输入的是文本而不是数字)后能够继续输入数字。
l = [0, 0] for i in range(0, 2): while True: try: l[i] = int(input("请输入一个数字:")) except ValueError: print('你输入的不是数字!请重新输入') else: break print('和为:' + str(sum(l)))输出:
请输入一个数字:5 请输入一个数字:gg 你输入的不是数字!请重新输入 请输入一个数字:mm 你输入的不是数字!请重新输入 请输入一个数字:哈哈 你输入的不是数字!请重新输入 请输入一个数字:6 和为:11
10-10 常见单词 :访问项目Gutenberg(http://gutenberg.org/ ),并找一些你想分析的图书。下载这些作品的文本文件或将浏览器中的原始文本复制到文本文件中。 你可以使用方法count() 来确定特定的单词或短语在字符串中出现了多少次
请注意,通过使用lower() 将字符串转换为小写,可捕捉要查找的单词出现的所有次数,而不管其大小写格式如何。 编写一个程序,它读取你在项目Gutenberg中获取的文件,并计算单词'the' 在每个文件中分别出现了多少次。
with open('MyMystic.txt') as f: cnt = f.read().lower().count('the') print('"the"一共出现' + str(cnt) + '次')
原文链接:http://www.gutenberg.org/files/56908/56908-0.txt
输出:
"the"一共出现2668次
10-11 喜欢的数字 :编写一个程序,提示用户输入他喜欢的数字,并使用json.dump() 将这个数字存储到文件中。再编写一个程序,从文件中读取这个值,并打印 消息“I know your favorite number! It's _____.”。
10-12 记住喜欢的数字 记住喜欢的数字 :将练习10-11中的两个程序合而为一。如果存储了用户喜欢的数字,就向用户显示它,否则提示用户输入他喜欢的数字并将其存储到文件中。 运行这个程序两次,看看它是否像预期的那样工作。
from json import * try: f = open('favourite_num.txt') except FileNotFoundError: nums = [] gets = input('请输入你喜欢的数字,以空格分隔,回车结束:') for i in gets.split(): try: nums.append(int(i)) except: pass with open('favourite_num.txt', 'w') as fout: dump(nums, fout) else: print('我知道你喜欢什么数字!,它们是:') for i in load(f): print(i, end = ' ') f.close()输出:
PS D:\Resource\Code\python> python a.py 请输入你喜欢的数字,以空格分隔,回车结束:5 6 8 2 3 hh gg mm 哈哈 PS D:\Resource\Code\python> python a.py 我知道你喜欢什么数字!,它们是: 5 6 8 2 3 PS D:\Resource\Code\python>favourite_nums.txt:
[5, 6, 8, 2, 3]