Python编程快速上手--实践项目参考答案
漠血依人
这个作者很懒,什么都没留下…
展开
专栏收录文章
- 默认排序
- 最新发布
- 最早发布
- 最多阅读
- 最少阅读
-
第7章 正则表达式之匹配手机号
import remessage = 'Call me at 415-555-1011 tomorrow. 415-555-9999 is my office.'def isPhoneNumber(chunk): PhoneNumberRegex = re.compile('\\d{3}-\\d{3}-\\d{4}') mo = PhoneNumberRegex.search(...原创 2018-06-15 09:48:00 · 195 阅读 · 0 评论 -
第11章 链接验证
import requests,bs4,osurl = "http://www.baidu.com"os.makedirs('xkcd',exist_ok=True)res = requests.get(url)res.raise_for_status()resHtml = bs4.BeautifulSoup(res.text,'html.parser')linkUrl = resH...原创 2018-08-03 17:04:12 · 324 阅读 · 0 评论 -
第11章:循环打开搜索关键字的网页
import requests,sys,webbrowser,bs4print("========searching========")res = requests.get("http://www.baidu.com/s?wd=python")res.raise_for_status() #检查请求是否成功,若成功,什么也不做;若失败,则抛出异常。#todo:retrieve top s...原创 2018-08-01 09:50:18 · 248 阅读 · 0 评论 -
第11章:获取html内容
#gain the text of <Romeo and juliet>import requests#罗密欧和朱丽叶的下载地址res = requests.get('http://www.gutenberg.org/files/1112//1112.txt')try: res.raise_for_status()#如果下载网页资源成功则,什么也不做,失败则抛出异常...原创 2018-08-01 10:20:02 · 281 阅读 · 0 评论 -
第12章 读取人口普查Excel数据
import openpyxl,pprintprint("=====打开Excel=====")wb = openpyxl.load_workbook("censuspopdata.xlsx")sheet = wb['Population by Census Tract']countryData = {}#Todo: fill in countryDate with each coun...原创 2018-08-06 16:00:29 · 1707 阅读 · 0 评论 -
第11章 下载xkcd的漫画
#! python3# downloadXkcd.py - Downloads every single XKCD comic.import requests, os, bs4url = 'http://xkcd.com' # starting url下载图片的网址os.makedirs('xkcd', exist_ok=True) # store comics in ./xkcdw...原创 2018-08-02 17:17:21 · 419 阅读 · 0 评论 -
第12章 更新商品价格表
import openpyxlwb = openpyxl.load_workbook("produceSales.xlsx")sheet = wb['Sheet']product_price = {'Garlic': 3.07, 'Celery': 1.19, 'Lemon': 1.27}for row in range...原创 2018-08-07 11:23:41 · 200 阅读 · 0 评论 -
第11章:2048联系键盘上下左右键
from selenium.webdriver.common.keys import Keysfrom selenium import webdriverdriver = webdriver.Ie()driver.get(' https://gabrielecirulli.github.io/2048/')html = driver.find_element_by_tag_name('h...原创 2018-08-03 15:37:57 · 611 阅读 · 0 评论 -
第12章 Excel插入空白行
#方法一思路:先复制前N 行的内容写入到新表格;②将N行之后内容行号加上M后,再写入新的电子表格import openpyxlfrom openpyxl.utils import get_column_letterdef blankRowInsert(n,m): wb = openpyxl.load_workbook("style.xlsx")#原始表格 wb2 = ope...原创 2018-08-13 13:56:54 · 733 阅读 · 1 评论 -
第12章项目 乘法表
import openpyxlfrom openpyxl.styles import Font,colorsdef multi(num): wb = openpyxl.Workbook() sheet = wb['Sheet'] FontStyle = Font(name='微软雅黑',size=12,color=colors.RED,bold=True) ...原创 2018-08-08 17:44:54 · 212 阅读 · 0 评论 -
第12章 将某目录下所有文本读取到Excel中
import os,reimport openpyxl#读取目录下的所有txt文件wb = openpyxl.Workbook()sheet = wb.activefilePath = 'E:\\04.AutomationProject\\PracticePython\\'pathDir = os.walk(filePath)i=1for root, subFolders, fi...原创 2018-08-27 10:11:04 · 257 阅读 · 0 评论 -
第12章 将Excel内容写入文本
import openpyxlfrom openpyxl.utils import get_column_letterwb = openpyxl.load_workbook("style.xlsx")sheets = wb.worksheetsfor st in sheets: fileName = str(st)[12:-2]+".txt" fp = open(file...原创 2018-08-27 13:48:39 · 312 阅读 · 0 评论 -
第13章 从多个PDF中合并选择的页面
import os,PyPDF2path = "E:\\04.AutomationProject\\PracticePython\\"pdfWriter = PyPDF2.PdfFileWriter()for currentFolder,subFoders,fileNames in os.walk(path): for fileName in fileNames: ...原创 2018-08-28 18:05:07 · 280 阅读 · 0 评论 -
第14章项目:获取的上海当前天气数据
# 在dos命令下输入:python36 test.py 上海import json, requests, sys# Compute location from command line arguments.# if len(sys.argv) < 2:# print('Usage: quickWeather.py location')# sys.exit()#...原创 2019-02-21 18:13:02 · 367 阅读 · 0 评论 -
第13章 PDF偏执狂
#第十三章 PDF偏执狂#①使用os.work()函数遍历文件夹下所有pdf文件,存储在fileList[]中#②判断这些pdf是否已经加密,如果没有是,顺次读取这些pdf并用123456加密#③给加密后的pdf以原名称加上_encrypted后缀重命名,保存为新的文件#④读取这些加密后的pdf,并用12345解密,确保加密的正确性#⑤如果口令错误,则输出“密码输入错误,错误文件为:x...原创 2019-02-18 16:17:48 · 276 阅读 · 0 评论 -
第13章实践项目:定制邀请函,保存为 Word 文档
#第十三章实践项目:定制邀请函,保存为 Word 文档#可以单独设置字体doc.paragraphs[i].style.font.name = 'Harlow Solid Italic'#docx只能引用word本身存在的样式,以下代码可查询内置可用的样式:'''from docx.enum.style import WD_STYLE_TYPEfrom docx import Docum...原创 2019-02-19 10:06:40 · 564 阅读 · 0 评论 -
第13章:暴力 PDF 口令破解程序
#第十三章:暴力 PDF 口令破解程序import PyPDF2pwdFile = open('dictionary.txt','r')pwdList=pwdFile.readlines()pdfReader = PyPDF2.PdfFileReader(open('encryptedMinutes.pdf','rb'))pwdDic = []for pwd in pwdList:...原创 2019-02-19 14:18:58 · 1461 阅读 · 0 评论 -
第14章实践项目:Excel到csv的转换
#第14章实践项目:Excel到csv的转换import openpyxl,os,csvfor excelFile in os.listdir('.\\headerRemoved'): if not excelFile.endswith('.xlsx'): continue print('===Transforming ' + excelFile + ' to...原创 2019-02-26 11:23:00 · 292 阅读 · 0 评论 -
第10章:调试抛掷硬币
#第10章:调试抛掷硬币import random,logginglogging.basicConfig(level=logging.DEBUG, format=('%(asctime)s--%(levelname)s--%(message)s'))# logging.disable(logging.DEBUG)logging.debug('\n============start of ...原创 2018-07-24 17:24:02 · 466 阅读 · 0 评论 -
第9章 消除缺失的编号
#python3import os,re,shutilpath = 'E:\\04.AutomationProject\\PracticePython\\noteBasic'os.chdir(path)#找到指定文件夹中所有哦带指定前缀的文件fileNameList = [i for i in os.listdir('.') if i.startswith('spam') and i.e...原创 2018-07-12 10:38:47 · 662 阅读 · 0 评论 -
第9章 删除不需要的文件
'''编写一个程序, 遍历一个目录树, 查找特别大的文件或文件夹, 比如, 超过100MB 的文件(获取文件大小,os.path.getsize())。将这些文件的绝对路径打印到屏幕上'''import os,send2trashpath = 'E:\\04.AutomationProject'for currentFolder,subFolder,fileNames in os.walk(...原创 2018-07-09 17:43:32 · 275 阅读 · 0 评论 -
第6章 字符串操作之表格打印
#表格打印:tableData = [['apples', 'oranges', 'cherries', 'banana'], ['Alice', 'Bob', 'Carol', 'David'], ['dogs', 'cats', 'moose', 'goose']]def printTable(): #计算列表每行最大字符长度的集...原创 2018-06-15 09:50:45 · 305 阅读 · 0 评论 -
第6章 字符串处理之口令保管箱
# pw.py - An insecure password lockimport sysimport pyperclipPASSWORDS = {'email': 'F7minlBDDuvMJuxESSKHFhTxFtjVB6', 'blog': 'VmALvQyKAxiVH5G8v01if1MLZF3sdt', 'luggage': '...翻译 2018-06-15 09:53:27 · 659 阅读 · 0 评论 -
第5章 字典之物品清单
dragonLoot = ['gold coin', 'dagger', 'gold coin', 'gold coin', 'ruby']inv = {'gold coin': 42, 'rope': 1}def displayInventory(inv): count =0 for k,v in inv.items(): print(k + " "+ str...原创 2018-06-15 09:55:06 · 233 阅读 · 0 评论 -
第5章 字典之物品清单2
# 5.6.2#将给定的列表添加到字典中去,并统计相同键对应的数量,最后统计总字典中值的总数dragonLoot = ['gold coin', 'dagger', 'gold coin', 'gold coin', 'ruby']print("5.6.2参考答案")print('=' * 80)#分割线inv = {'gold coin':42,'rope':1}print("给定列...转载 2018-06-15 09:56:38 · 176 阅读 · 0 评论 -
第4章 列表之打印井字棋盘
#打印井字棋盘theBoard = {'top-L': ' ','top-M': ' ','top-R': ' ', 'mid-L': ' ', 'mid-M': '', 'mid-R': ' ', 'low-L': ' ', 'low-M': ' ', 'low-R': ' '}def printBoard(board): print...翻译 2018-06-15 09:58:05 · 603 阅读 · 0 评论 -
第4章 列表之字符图网格
grid = [['.', '.', '.', '.', '.', '.'],['.', 'O', 'O', '.', '.', '.'],['O', 'O', 'O', 'O', '.', '.'],['O', 'O', 'O', 'O', 'O', '.'],['.', 'O', 'O', 'O', 'O', 'O'],['O', 'O', 'O', 'O', 'O', '.'],...原创 2018-06-15 10:00:19 · 426 阅读 · 1 评论 -
第9章 创建和添加zip文件
#创建和添加压缩文件import zipfile,os,datetimepath = 'E:\\04.AutomationProject\\PracticePython\\noteBasic'os.chdir(path)#方法一:这种方法会把多层级的目录放入压缩包# fileList = []# for folderName, subFolders, fileNames in os.w...原创 2018-06-29 16:26:27 · 239 阅读 · 0 评论 -
第7章 抽取电话和email
#phone number and email address are extratedarticle='##refsfs:800*420*7240 ext 3232 fsdfsdfsd sdfsd:415-863-9900xcvxvdscs litjtg:(415).863.9950swqfwvz ' \ 'gpojorjo:info@nostarch.com sdfsn...原创 2018-06-19 11:03:56 · 505 阅读 · 0 评论 -
第7章 强口令监测
#强口令监测import redef length(Password): if len(Password) < 8: print("the password is too short,please input again:") return False number = re.compile(r'\d+') if number.se...原创 2018-06-19 14:39:24 · 337 阅读 · 0 评论 -
第7章 使用sub()写出strip()的功能
'''sub()参数解析:①pattern:表示正则中的模式字符串②repl:表示替换成什么字符串③string:表示被处理的字符串④count:表示负荷pattern的前多少个字串⑤flags:编译时用的匹配模式 如re.I'''import redef CrestStrip(str1,str2=""): if str2=="": newStr = re...原创 2018-06-19 15:23:51 · 193 阅读 · 0 评论 -
第8章 扩展多重剪贴板
#!test.py# -*- coding: utf-8 -*-# mcb.pyw - saves and loads pieces of text to the clipboard# usage: python test.py save <keyword> - saves clipboard to keyword# python test.py <keywo...原创 2018-06-26 16:21:26 · 959 阅读 · 0 评论 -
第8章 疯狂填词
#疯狂填词import retext = 'The ADJECTIVE panda walked to the NOUN and then VERB. A nearby NOUN was unaffected by these events.'repTargetList = re.compile(r'[A-Z]{2,}').findall(text)print(repTargetList)...原创 2018-06-26 16:38:41 · 545 阅读 · 0 评论 -
第8章 正则表达式查找文件内容
import os, repath = 'E:\\04.AutomationProject\\PracticePython'folder = os.path.exists(path)if folder: print("目录正确!")else: print("目录不存在!")fileNameList = os.listdir(path)for file in fileNa...原创 2018-06-26 17:02:12 · 1658 阅读 · 0 评论 -
第8章 生成随机的测试试卷
#第八章项目:生成随机的测试试卷#第一步:将测验数据保存在一个字典中import randomcapitals = {'Alabama': 'Montgomery', 'Alaska': 'Juneau', 'Arizona': 'Phoenix', 'Arkansas': 'Little Rock', 'California': 'Sacramento', 'Col...原创 2018-06-20 11:44:54 · 642 阅读 · 0 评论 -
第9章 文件遍历
import osfor folderName, subFolders, fileNames in os.walk('E:\\04.AutomationProject\\PracticePython'): print('The current folder is ' + folderName) for subFolder in subFolders: print(...原创 2018-06-27 10:41:39 · 139 阅读 · 0 评论 -
第9章 文件压缩
#文件压缩import zipfile,osos.chdir('E:\\04.AutomationProject\\PracticePython\\noteBasic')# print(os.listdir())exampleZip = zipfile.ZipFile('example.zip')print(exampleZip.namelist())spamInfo =example...原创 2018-06-27 11:10:01 · 144 阅读 · 0 评论 -
第9章 选择性拷贝
import os,re,shutilpath = 'E:\\04.AutomationProject'fileNameList = os.listdir(path)folder = "E:\\test"if os.path.exists(folder): print("目录存在")else: os.mkdir("E:\\test") print("目录创建成功...原创 2018-07-09 17:15:32 · 527 阅读 · 0 评论 -
第15章实践项目:超级秒表
#第15章实践项目:超级秒表import timeprint('press ENTER to begin.Afterwards,press ENTER to "click" the stopwatche. Press Ctrl-c to quit')input()print('Started.')startTime = time.time()lastTime = startTime...原创 2019-02-26 15:57:55 · 377 阅读 · 1 评论
分享