
python练习
shifanfashi
这个作者很懒,什么都没留下…
展开
-
第 0007 题: 有个目录,里面是你自己写过的程序,统计一下你写过多少行代码。包括空行和注释,但是要分别列出来。
import os# os 模块用来处理文件夹及文件目录import stringimport renum_code = 0num_empty = 0num_note = 0os.chdir('E:\python\wenjian\ex-2')# os.chdir 用来改变当前处理路径f = open('spider_maoyan.py')read_f = f.readli...原创 2019-04-20 17:31:03 · 840 阅读 · 0 评论 -
第 0018 题: 将 第 0015 题中的 city.xls 文件中的内容写到 city.xml 文件中,如下 所示:
<root><cities><!-- 城市信息-->{ "1" : "上海", "2" : "北京", "3" : "成都"}</cities></root>import xlrdimport jsonfrom lxml import etreedef read_exl(file_name): ...原创 2019-04-24 09:22:16 · 453 阅读 · 0 评论 -
LeetCode 第二题 Add Two Numbers
You are given two non-empty linked lists representing two non-negative integers. The digits are stored in reverse order and each of their nodes contain a single digit. Add the two numbers and return i...原创 2019-04-28 14:01:24 · 344 阅读 · 0 评论 -
第 0006 题: 你有一个目录,放了你一个月的日记,都是 txt,为了避免分词的问题,假设内容都是英文,请统计出你认为每篇日记最重要的词。
import os# os 模块提供了非常丰富的方法用来处理文件和目录。详细方法请参考菜鸟教程。import rere模块为正则表达式模块def findWord(DirPath):# 定义 ******函数 if not os.path.isdir(DirPath): return fileList = os.listdir(DirPath)...原创 2019-04-19 16:04:06 · 262 阅读 · 0 评论 -
第 0002 题: 将 0001 题生成的 200 个激活码(或者优惠券)保存到 MySQL 关系型数据库中。
from random import choiceimport stringimport pymysql.cursorsdef get_code(dict, length, count):#根据给定字典,长度来得出激活码 for i in range(1,int(count)+1): code = "" #通过count限制激活码个数,循环调用choice...原创 2019-04-19 22:49:30 · 1092 阅读 · 0 评论 -
第 0003 题: 将 0001 题生成的 200 个激活码(或者优惠券)保存到 Redis 非关系型数据库中。
import redisimport randomr=redis.Redis(host='127.0.0.1',port=6379,db=0)list=[]#生成26个大写的字母for x in range(65,91): a=str(chr(x)) #生成对应的ASCII码对应的字符串 list.append(a)#生成26个小写字母for x in rang...原创 2019-04-20 09:25:34 · 489 阅读 · 0 评论 -
LeetCode 第三题Longest Substring Without Repeating Characters
Given a string, find the length of the longest substring without repeating characters.Example 1:Input: “abcabcbb”Output: 3Explanation: The answer is “abc”, with the length of 3.Example 2:Input: ...原创 2019-04-29 15:55:36 · 340 阅读 · 0 评论 -
第 0019 题: 将 第 0016 题中的 numbers.xls 文件中的内容写到 numbers.xml 文件中,
第 0019 题: 将 第 0016 题中的 numbers.xls 文件中的内容写到 numbers.xml 文件中,如下所示:<?xml version="1.0" encoding="UTF-8"?>[ [1, 82, 65535], [20, 90, 13], [26, 809, 1024]]"""import xlrd# pyt...原创 2019-04-25 11:11:09 · 466 阅读 · 0 评论 -
第 0020 题: 登陆中国联通网上营业厅 后选择「自助服务」 「详单查询」,然后选择你要查询的时间段,点击「查询」按钮,查询结果页面的最下方,
第 0020 题: 登陆中国联通网上营业厅 后选择「自助服务」 --> 「详单查询」,然后选择你要查询的时间段,点击「查询」按钮,查询结果页面的最下方,点击「导出」,就会生成类似于 2014年10月01日~2014年10月31日通话详单.xls 文件。写代码,对每月通话时间做个统计。import xlrddef count_the_daily_time(filename): e...原创 2019-04-25 11:15:01 · 1489 阅读 · 0 评论 -
第 0021 题: 通常,登陆某个网站或者 APP,需要使用用户名和密码。密码是如何加密后存储起来的呢?请使用 Python 对密码加密。
import osfrom hashlib import sha256from hmac import HMACdef encrypt_password(password, salt=None): if salt is None: salt = os.urandom(8) assert 8 == len(salt) assert isinstance...原创 2019-04-25 11:16:22 · 1249 阅读 · 0 评论 -
第 0022 题: iPhone 6、iPhone 6 Plus 早已上市开卖。请查看你写得 第 0005 题的代码是否可以复用
import osfrom PIL import ImagePHONE = {'iPhone5':(1136,640), 'iPhone6':(1134,750), 'iPhone6P':(2208,1242)}def resize_pic(path, new_path, phone_type): im = Image.open(path) w,h = im.size...原创 2019-04-25 11:17:37 · 440 阅读 · 0 评论 -
第 0023 题: 使用 Python 的 Web 框架,做一个 Web 版本 留言簿 应用。
import sqlite3from flask import Flask, request, session, g, redirect, url_for, abort, render_template, flashfrom contextlib import closingimport timeDATABASE = 'guestbook.db'DEBUG = TrueSECRET_...原创 2019-04-25 11:18:34 · 782 阅读 · 0 评论 -
第 0005 题: 你有一个目录,装了很多照片,把它们的尺寸变成都不大于 iPhone5 分辨率的大小。
from PIL import Image#PIL是Python Imaging Library 是图像存档和批处理应用程序的理想选择。您可以使用该库创建缩略图,在文件格式之间进行转换,打印图像等。import os.path#os.path 模块主要用于获取文件的属性。def Size(dirPath, size_x, size_y):#定义函数 f_list = os.li...原创 2019-04-19 10:02:18 · 505 阅读 · 0 评论 -
LeetCode 第一题two sum (python)
Given an array of integers, return indices of the two numbers such that they add up to a specific target.You may assume that each input would have exactly one solution, and you may not use the same e...原创 2019-04-28 10:10:57 · 384 阅读 · 0 评论 -
show me the code 第 0000 题: 将你的 QQ 头像(或者微博头像)右上角加上红色的数字,类似于微信未读信息数量那种提示效果
from PIL import Image, ImageDraw, ImageFontdef add_num(image): draw = ImageDraw.Draw(image) font = ImageFont.truetype('C:/windows/fonts/arial.ttf', size=50) fillcolor = "#f0000f" w...原创 2019-04-18 18:02:22 · 388 阅读 · 0 评论 -
第 0008 题: 一个HTML文件,找出里面的正文。
from bs4 import BeautifulSoup# with open("e:\python\wenjian\ex-2\venv\lol-第1页.html", "r", encoding="utf-8") as f:with open("E:\python\wenjian\ex-2\html1.html", "r", encoding="utf-8") as f:# html_...原创 2019-04-20 18:03:00 · 449 阅读 · 0 评论 -
第 0009 题: 一个HTML文件,找出里面的链接。
import reimport osdef handle_html(file_name): print(os.getcwd()) line = open(file_name, 'r', encoding='utf-8').read() pattern = (r'([hftps]+://[^\s]*)"') for i in (re.findall(patte...原创 2019-04-20 18:53:30 · 300 阅读 · 0 评论 -
第 0010 题:使用 Python 生成类似于下图中的字母验证码图片
from PIL import Image, ImageFilter, ImageFontimport random# ImageFilter:Python中的图像滤波,主要对图像进行平滑、锐化、边界增强等滤波处理。def random(): if random.randint(0, 1) == 0: return(chr(random.randint(65,90...原创 2019-04-20 19:45:45 · 1088 阅读 · 0 评论 -
阅读资料 第 0011 题: 敏感词文本文件 filtered_words.txt,
阅读资料 第 0011 题: 敏感词文本文件 filtered_words.txt,里面的内容为以下内容,当用户输入敏感词语时,则打印出 Freedom,否则打印出 Human Rights。 北京 程序员 公务员 领导 牛比 牛逼 你娘 你妈 love sex jianggedef get_filters(path): if path is None: ...原创 2019-04-20 20:48:17 · 1014 阅读 · 0 评论 -
第 0012 题: 敏感词文本文件 filtered_words.txt,里面的内容 和 0011题一样,当用户输入敏感词语,则用 星号 * 替换,
import redef get_filters(path): if path is None: return filters = ["北京", "程序员", "公务员", "领导", "牛比", "牛逼", "你娘", "你妈", "love", "sex", "jiangge"] with open(path,encoding="utf-8") as ...原创 2019-04-20 21:47:21 · 1030 阅读 · 0 评论 -
第 0013 题: 用 Python 写一个爬图片的程序,爬 这个链接里的日本妹子图片 :-)
日本妹子图片连接from urllib.error import URLError, HTTPErrorimport urllib.parseimport urllib.requesturl = 'http://tieba.baidu.com/p/2166231880'values = {'wd': 'python', 'opt-webpage': 'on', ...原创 2019-04-21 09:45:45 · 486 阅读 · 2 评论 -
第 0014 题: 纯文本文件 student.txt为学生信息, 里面的内容(包括花括号)
第 0014 题: 纯文本文件 student.txt为学生信息, 里面的内容(包括花括号)如下所示:{ "1":["张三",150,120,100], "2":["李四",90,99,95], "3":["王五",60,66,68]}请将上述内容写到 student.xls 文件中,如下图所示:import osimport jsonimport xlwt# xlwt是...原创 2019-04-21 17:57:10 · 5260 阅读 · 0 评论 -
第 0015 题: 纯文本文件 city.txt为城市信息, 里面的内容(包括花括号)如下所示: { "1" : "上海", "2" : "北京", "3" : "成都" }
**第 0015 题: 纯文本文件 city.txt为城市信息, 里面的内容(包括花括号)如下所示:{ “1” : “上海”, “2” : “北京”, “3” : “成都” } 请将上述内容写到 city.xls 文件中,如下图所示:city.xls**port osimport jsonimport xlwtdef read_txt(path): with open(pat...原创 2019-04-21 18:08:40 · 1918 阅读 · 0 评论 -
第 0016 题: 纯文本文件 numbers.txt, 里面的内容(包括方括号)如下所示: [ [1, 82, 65535], [20, 90, 13], [26, 809, 1024] ]
第 0016 题: 纯文本文件 numbers.txt, 里面的内容(包括方括号)如下所示:[ [1, 82, 65535], [20, 90, 13], [26, 809, 1024] ] 请将上述内容写到 numbers.xls 文件中,如下图所示:numbers.xlsimport jsonimport xlwtwb = xlwt.Workbook()ws = wb.add_...原创 2019-04-22 10:50:29 · 1379 阅读 · 0 评论 -
第 0017 题: 将 第 0014 题中的 student.xls 文件中的内容写到 student.xml 文件中,如 下所示:
第 0017 题: 将 第 0014 题中的 student.xls 文件中的内容写到 student.xml 文件中,如下所示:<?xml version="1.0" encoding="UTF-8"?>{ "1" : ["张三", 150, 120, 100], "2" : ["李四", 90, 99, 95], "3" : ["王五", 60, 66, 68...原创 2019-04-22 11:24:49 · 1879 阅读 · 1 评论 -
python练习题 show me code 0001
第 0001 题:做为 Apple Store App 独立开发者,你要搞限时促销,为你的应用生成激活码(或者优惠券),使用 Python 如何生成 200 个激活码(或者优惠券)?import base64# base64编码方便使用# 通过id检验优惠券是否存在,通过goods查找商品coupon = { 'id': '1231', 'goods': '0001',...原创 2019-04-18 17:08:16 · 315 阅读 · 0 评论 -
每天一个python小程序 0004:任一个英文的纯文本文件,统计其中的单词出现的个数
import re# 文件名可以替换,且需要在工程目录下file_name = 'coupon.txt'lines_count = 0words_count = 0chars_count = 0words_dict = {}lines_list = []with open(file_name, 'r', encoding="utf-8") as f: for line...原创 2019-04-18 18:00:24 · 839 阅读 · 0 评论