![](https://img-blog.csdnimg.cn/20201014180756918.png?x-oss-process=image/resize,m_fixed,h_224,w_224)
python练习
《Python编程:从入门到实践》练习
学技术的翻译小白
记录生活,记录python学习之路。
展开
-
中文文本词云生成
参考来源地址:123特此鸣谢注意:原文需保存为.txt文本,utf-8编码图片背景应提供.png格式代码文件无需和文本、图片放在同一目录,运行时会提示输入路径运行前请安装wordcloud,imageio,matplotlib,jieba相关库from wordcloud import WordCloudimport imageioimport matplotlib.pyplot as pltimport jieba,timedef read_deal_text(.原创 2021-08-30 08:09:39 · 369 阅读 · 0 评论 -
python寻找网站以“/“开头的链接
from urllib.request import urlopenfrom bs4 import BeautifulSouphtml = urlopen('https://www.oreilly.com/')bs = BeautifulSoup(html.read(), 'html.parser')links = []for a in bs.find_all('a'): link = a['href'] links.append(link)print(links)print原创 2021-01-05 22:59:59 · 114 阅读 · 0 评论 -
《Python编程:从入门到实践》练习10-13 验证用户
针对以下情况进行了优化:用户已输入用户名用户首次运行程序,未输入用户名用户非首次运行,但文件已写入数据import jsondef get_stored_username(): """如果存储了用户名,就获取它""" filename = 'username.json' try: with open(filename) as f: username = json.load(f) except FileNotFoundE原创 2020-12-30 22:47:13 · 178 阅读 · 0 评论 -
《Python编程:从入门到实践》练习10-12 记住喜欢的数
import jsondef get_stored_number(): """如果存储了,就获取它""" filename = 'favorite_number.json' try: with open(filename) as f: favorite_number = json.load(f) except FileNotFoundError: return None else: retu原创 2020-12-30 20:54:34 · 193 阅读 · 0 评论 -
《Python编程:从入门到实践》练习10-11 喜欢的数
import jsondef get_favorite_number(): """获取用户输入的数字""" filename = 'favorite_number' favorite_number = input("Please tell me your favorite number: ") with open(filename, 'w') as f: json.dump(favorite_number, f)def check_number原创 2020-12-30 20:39:22 · 254 阅读 · 0 评论 -
《Python编程:从入门到实践》10.4 储存数据
import jsonfilename = 'username.json'# 如果以前储存了用户,就加载它try: with open(filename) as f: username = json.load(f)# 如果没有储存过用户,则提示用户输入用户名并存储它except FileNotFoundError: username = input("What is your name?") with open(filename, 'w') as f原创 2020-12-29 18:05:43 · 108 阅读 · 0 评论 -
《Python编程:从入门到实践》10-10 计算单词频数
计算某一词在文本中出现的次数计算某一词(去除大小写)在文本中出现的次数>>>line = 'Row, row, row your boat'>>>line.count('row')2>>>line.lower().count('row')3def count_word(filename, word): try: with open(filename, encoding='UTF-8') as f: .原创 2020-12-28 23:27:11 · 162 阅读 · 0 评论 -
《Python编程:从入门到实践》10.8 10.9 猫和狗
创建两个文件读取文件内容,并将其内容打印在文件不存在时显示一条消息确认excep代码块filename_1 = 'cats.txt'filename_2 = 'dogs.txt'def open_file(filename): try: with open(filename) as f: contents = f.read() print(contents) except FileNotFoundError:.原创 2020-12-28 22:58:14 · 250 阅读 · 0 评论 -
《Python编程:从入门到实践》10.6 10.7 加法运算
达成效果:用户输入两个数,对其做加法运算求和在用户输入文本而非数字时,打印一条提示信息print("Please enter two numbers, and I'll add them.")while True: try: first_number = input("Please enter the first number: ") number_1 = float(first_number) except ValueError: .原创 2020-12-28 22:49:08 · 209 阅读 · 0 评论 -
《Python编程:从入门到实践》练习10-3访客
names = input('Please tell me your name: ')filename = 'guests.txt'with open('guests.txt', 'a') as file_object: file_object.write(names)原创 2020-12-27 22:28:22 · 285 阅读 · 0 评论 -
《Python编程:从入门到实践》练习10-2
练习10-2print(p_string)`file_name = ‘learning_python.txt’with open(file_name, encoding=‘utf-8’) as fo:lines = fo.readlines()p_string = ‘’for line in lines:if ‘翻译’ in line:new_line = line.replace(‘翻译’, ‘translation’) # 要将替换后的字符串赋给新的变量,否则不起效果p_strin原创 2020-12-27 14:36:08 · 165 阅读 · 0 评论 -
《Python编程:从入门到实践》练习10-4 访客名单
编写一个while 循环, 提示用户输入其名字。用户输入其名字后, 在屏幕上打印一句问候语, 并将一条访问记录添加到文件guest_book.txt中。确保这个文件中的每条记录都独占一行。filename = 'guest_book.txt'while True: name = input('Please tell me your name (Enter q to quit.): ') if name == 'q': break else:原创 2020-12-27 22:44:18 · 603 阅读 · 0 评论