Python 中常用的标准库
-
python中获取当前时间和日期的模块:
datetime -
python中具有更改当前工作目录的方法
os -
哪个模块可以将逗号分隔 (.csv) 文件中的每行数据读取到 Python 中?
csv -
哪个模块可以帮助我们从 zip 文件中提取所有文件?
zipfile -
哪个模块可以显示代码的运行时间?
time
Python 中常用的标准库[相关练习]
-
使用
math模块,计算e的3次幂,然后 输出 答案import math print(math.exp(3)) # 20.085536923187668 -
写一个叫做
generate_password的函数,该函数会从提供的单词文件中随机选择三个单词,并将它们连接成一个字符串。我们已经在起始代码中提供了从文件中读取数据的代码,你需要利用这些部分构建一个密码。words.txt:
Alice was beginning to get very tired of sitting by her sister bank having nothing Once twice she had peeped into the book her sister was reading but it had no pictures or conversations in it and what is the use of a book thought Alice without pictures or conversationspassword_generator.py :
# Use an import statement at the top import random word_file = "words.txt" word_list = [] #fill up the word_list with open(word_file,'r') as words: for line in words: # remove white space and make everything lowercase word = line.strip().lower() # don't include words that are too long or too short if 3 < len(word) < 8: word_list.append(word) # Add your function generate_password here # It should return a string consisting of three random words # concatenated together without spaces def generate_password() : return random.choice(word_list) + random.choice(word_list) + random.choice(word_list) # test your function print(generate_password())运行 $
python password_generator.py, 随机输出三个单词连成的字符串
本文介绍了Python中的几个关键标准库,包括datetime用于处理时间和日期,os用于操作文件系统,csv用于解析CSV文件,zipfile用于解压ZIP文件,time用于记录代码运行时间。此外还提供了一个使用Python生成随机密码的示例。
3005

被折叠的 条评论
为什么被折叠?



