热爱Python Python是Guido van Rossum设计出来的让使用者觉得如沐春风的一门编程语言。2020年11月12日,64岁的Python之父宣布由于退休生活太无聊,自己决定加入Mic……继续阅读 » dongfanger 4年前 (2021-03-30) 1650浏览 0评论1557个赞
通过范例学习 PyTorch 本博文通过几个独立的例子介绍了 PyTorch 的基础概念。 其核心,PyTorch 提供了两个主要的特征: 一个 n-维张量(n-dimensional Tensor)……继续阅读 » 极客锋行 4年前 (2021-03-30) 1816浏览 0评论2695个赞
在人生苦短我用Python,本文助你快速入门这篇文章中,学习了Python的语法知识。现在我们就拿Python做个爬虫玩玩,如果中途个别API忘了可以回头看看,别看我,我没忘!(逃 网络编程 学习……继续阅读 » 耶low 4年前 (2021-03-30) 2800浏览 0评论189个赞
Django做网站只要10步,真的只有10步,不信?咱们来数数…… 今天主要讲解用Pycharm编辑器搭建网站,网站功能是 实现在局域网中快速传递大文件! 比如:同事要给你个1G的文件,你丢一个网站链……继续阅读 » 逗号下一站 4年前 (2021-03-30) 2913浏览 0评论1516个赞
随着互联网的不断发展,越来越多的程序员都在学习软件测试技术,而今天我们就一起来了解一下,软件测试框架与应用方法。……继续阅读 » 测试萌萌 4年前 (2021-03-23) 2530浏览 0评论2476个赞
python可以通过open函数打开文件,通过close函数关闭文件#!/usr/bin/python# Open a filefo = open("foo.txt", "wb")print "Name of the file: ", fo.name# Close opend fi……继续阅读 » 水墨上仙 4年前 (2021-03-22) 2478浏览 2131个赞
一个python自定义的加密解密函数,下面的代码实现了对字符串的加密盒解密,调用简单def encrypt(key, s): b = bytearray(str(s).encode("gbk")) n = len(b) # 求出 b 的字节数 c = bytearray(n*2) j = 0 ……继续阅读 » 水墨上仙 4年前 (2021-03-22) 1466浏览 1378个赞
python通过zlib压缩和解压缩字符串,可以自定义压缩级别,级别越高压缩的约厉害,效率要低一些import zlibcompressedString = zlib.compress(originalString, 9) # 采用9级压缩decompressedString = zlib.decompress(compressedStrin……继续阅读 » 水墨上仙 4年前 (2021-03-22) 2141浏览 1343个赞
python自定义isnumber函数判断字符串是否为数字''' isnumeric.pytest a numeric string s if it's usable for int(s) or float(s)'''def isnumeric(s): '&……继续阅读 » 水墨上仙 4年前 (2021-03-22) 2332浏览 1435个赞
python不定义函数实现数字增减,原来python还可以这么用,你一定想不到inc = (1).__add__print(inc(4))#5sub = (-1).__radd__print(sub(5))#4……继续阅读 » 水墨上仙 4年前 (2021-03-22) 2198浏览 1044个赞
python通过Tkinter显示网络图片''' tk_image_view_url_io.pydisplay an image from a URL using Tkinter, PIL and data_streamtested with Python27 and Python33 by vegaseat 0……继续阅读 » 水墨上仙 4年前 (2021-03-22) 1861浏览 1700个赞
python通过PyGame绘制图像并保存为图片文件''' pg_draw_circle_save101.pydraw a blue solid circle on a white backgroundsave the drawing to an image filefor result see http://prn……继续阅读 » 水墨上仙 4年前 (2021-03-22) 2197浏览 2665个赞
将这段代码放入你的脚本中,类似:urllib.urlretrieve(getFile, saveFile, reporthook=report)第三个参数如下面的函数定义report,urlretrieve下载文件时会实时回调report函数,显示下载进度def report(count, blockSize, totalSize): perc……继续阅读 » 水墨上仙 4年前 (2021-03-22) 1811浏览 1019个赞
python-memcache 基本使用方法代码,如果没有安装python-memcached可以先通过easy_install安装:easy_install python-memcached#!/usr/bin/env python import memcache mc = memcache.Client(['127.0.0.1:……继续阅读 » 水墨上仙 4年前 (2021-03-22) 1984浏览 1349个赞
python链接远程ftp服务器并列出目录下的文件,这段python代码用到了pysftp模块,使用sftp协议,对数据进行加密传输import pysftp srv = pysftp.Connection(host="your_FTP_server", username="your_username",p……继续阅读 » 水墨上仙 4年前 (2021-03-22) 1341浏览 477个赞
python通过BeautifulSoup分析网页信息,这段python代码查找网页上的所有链接,分析所有的span标签,并查找class包含titletext的span的内容#import the library used to query a websiteimport urllib2 #specify the url you want to……继续阅读 » 水墨上仙 4年前 (2021-03-22) 1923浏览 2995个赞
给定一个1-99之间的数,让用户猜数字,当用户猜错时会提示用户猜的数字是过大还是过小,知道用户猜对数字为止,猜对数字用的次数越少成绩越好。import randomn = random.randint(1, 99)guess = int(raw_input("Enter an integer from 1 to 99: "))……继续阅读 » 水墨上仙 4年前 (2021-03-22) 1740浏览 2305个赞
os是python用于操作系统级的操作模块,包括获取系统信息,文件及目录操作等import osos.system() #用于执行系统命令os.environ() #返回用户环境信息os.getcwd() #返回当前工作目录os.getgid() #返回当先进程的group idos.getuid() #返回当前进程的用户id……继续阅读 » 水墨上仙 4年前 (2021-03-22) 2914浏览 1228个赞
python统计字符串中指定字符出现的次数,例如想统计字符串中空格的数量s = "Count, the number of spaces."print s.count(" ") x = "I like to program in Python"print x.count(&qu……继续阅读 » 水墨上仙 4年前 (2021-03-22) 1726浏览 870个赞
下面的python代码通过对各种字符进行随机组合生成一个指定长度的随机密码 python中的string对象有几个常用的方法用来输出各种不同的字符:string.ascii_letters输出ascii码的所有字符string.digits输出 ’……继续阅读 » 水墨上仙 4年前 (2021-03-22) 3036浏览 1269个赞
下面的python代码展示python中字典的常用操作,字典在python开发中有着举足轻重的地位,掌握字典操作相当重要#创建一空字典x = {}#创建包含三个项目的字典x = {"one":1, "two":2, "three":3}#访问其中的一个元素x['two……继续阅读 » 水墨上仙 4年前 (2021-03-22) 2695浏览 2125个赞
python实现的telnet客户端程序,python自带一个telnetlib模块,可以通过其Telnet类实现telnet操作import getpassimport sysimport telnetlib HOST = "hostname" user = raw_input("Enter your rem……继续阅读 » 水墨上仙 4年前 (2021-03-22) 2369浏览 2100个赞
给定网址,通过这段python代码可以很容易获取域名信息import urlparseurl = "http://www.75271.com"domain = urlparse.urlsplit(url)[1].split(':')[0]print "The domain name of the……继续阅读 » 水墨上仙 4年前 (2021-03-22) 2253浏览 851个赞
这段python代码通过urllib2抓取网页,然后通过简单的正则表达式分析网页上的全部url地址import urllib2import re #connect to a URLwebsite = urllib2.urlopen(url) #read html codehtml = website.read() #use re.fi……继续阅读 » 水墨上仙 4年前 (2021-03-22) 2423浏览 1434个赞
python通过calendar输出指定年份的全年日历import calendar print "Show a given years monthly calendar" print '' year = int(raw_input("Enter the year")) pri……继续阅读 » 水墨上仙 4年前 (2021-03-22) 2347浏览 239个赞
下面python代码通过urllib2抓取指定的url的内容,并且使用自定义的user-agent,防止网站屏蔽采集器import urllib2 req = urllib2.Request('http://192.168.1.2/') req.add_header('User-agent', '……继续阅读 » 水墨上仙 4年前 (2021-03-22) 1321浏览 158个赞
下面的python代码可用于检测字符串,包括是否全部为数字,是否包含数字,是否包含标题单词,是否包含大写字母,是否包含小写字母,是否包含空格,是否以指定的字符开头和结尾。my_string = "Hello World"my_string.isalnum() #检测所有字符是否都是数字my_string.isalpha() ……继续阅读 » 水墨上仙 4年前 (2021-03-22) 1677浏览 234个赞
python中列表元素连接方法join的用法 创建列表>>> music = ["Abba","Rolling Stones","Black Sabbath","Metallica"……继续阅读 » 水墨上仙 4年前 (2021-03-22) 2564浏览 2727个赞
python常用列表(数组)操作演示,包括创建列表,添加新元素,获取列表长度,删除元素,数组翻转,列表排序,在指定位置添加元素,统计某元素出现的次数等等s = ['h','e','l','l','o'] #create a lists.append(&……继续阅读 » 水墨上仙 4年前 (2021-03-22) 1943浏览 1786个赞
python从显示ip地址的网站获取本机外网ip,这段python代码抓取网站上的ip地址信息import urllibimport re print "we will try to open this url, in order to get IP Address" url = "http://checkip.……继续阅读 » 水墨上仙 4年前 (2021-03-22) 2246浏览 543个赞
python开发的简单的猜单词游戏代码,这是一个简单的人机交互的小游戏,用户有10次机会来猜对单词,如果用户猜对了其中的一个字母,系统会在正确的位置显示该字母。#importing the time moduleimport time #welcoming the username = raw_input("What is your ……继续阅读 » 水墨上仙 4年前 (2021-03-22) 2678浏览 2190个赞
python根据用户输入从电影网站获取影片信息的范例代码,这段python代码主要演示了用户终端输入,正则表达式,网页抓取等#!/usr/bin/env python27 #Importing the modules from BeautifulSoup import BeautifulSoupimport sysimport urllib……继续阅读 » 水墨上仙 4年前 (2021-03-22) 3195浏览 2480个赞
通过这段代码可以一目了然的知道scrapy的抓取页面结构,调用也非常简单# This is a script to print the crawl tree of spider run.# # 使用方法:# # $ python ctree.py myspider.log# None# http://www.e……继续阅读 » 水墨上仙 4年前 (2021-03-22) 1747浏览 298个赞
scrapy自定义pipeline类将采集数据保存到mongodb# Standard Python library imports # 3rd party modulesimport pymongo from scrapy import logfrom scrapy.conf import settingsfrom scrapy.exc……继续阅读 » 水墨上仙 4年前 (2021-03-22) 1657浏览 2650个赞
通过scrapy实现的一个简单的蜘蛛采集程序# Standard Python library imports # 3rd party importsfrom scrapy.contrib.spiders import CrawlSpider, Rulefrom scrapy.contrib.linkextractors.sgml import……继续阅读 » 水墨上仙 4年前 (2021-03-22) 2996浏览 2161个赞
一个简单的python编写的猜数字游戏代码import randomguesses_made = 0name = raw_input('Hello! What is your name?\n')number = random.randint(1, 20)print 'Well, {0}, I am think……继续阅读 » 水墨上仙 4年前 (2021-03-22) 2200浏览 2602个赞
Queue模块允许创建指定长度的队列. 下面是Queue模块的常用方法:get():删除并返回队列中的一个项目put(): 添加项目到队列qsize() : 返回队列中元素的个数empty(): 队列为空返回True否则返回Falsefull(): 队列已满返回True,负责返回False#!/usr/bin/pythonimport Q……继续阅读 » 水墨上仙 4年前 (2021-03-22) 2824浏览 1404个赞
下面的python代码演示线程锁的用法和线程同步#!/usr/bin/pythonimport threadingimport timeclass myThread (threading.Thread): def __init__(self, threadID, name, counter): threading.Th……继续阅读 » 水墨上仙 4年前 (2021-03-22) 3211浏览 2781个赞
python多线程模块threading使用范例代码#!/usr/bin/pythonimport threadingimport timeexitFlag = 0class myThread (threading.Thread): def __init__(self, threadID, name, counter): ……继续阅读 » 水墨上仙 4年前 (2021-03-22) 3129浏览 1386个赞
python多线程模块thread使用范例#!/usr/bin/pythonimport threadimport time# Define a function for the threaddef print_time( threadName, delay): count = 0 while count < 5: ……继续阅读 » 水墨上仙 4年前 (2021-03-22) 1990浏览 2053个赞
python实现的简单的socket通信的客户端和服务器端 服务端代码#!/usr/bin/python # This is server.py fileimport socket # Import socket module……继续阅读 » 水墨上仙 4年前 (2021-03-22) 2295浏览 2612个赞
实际上返回的是计算机的cpu核心数,比如cpu是双核的,则返回2,如果双四核cpu,则返回8from multiprocessing import cpu_countprint(cpu_count()) 本机是四核电脑,返回结果:4……继续阅读 » 水墨上仙 4年前 (2021-03-22) 2793浏览 2960个赞
将Queue作为中间通道进行数据传递,Queue是线程和进程安全的from multiprocessing import Process, Queuedef f(q): q.put([42, None, 'hello'])if __name__ == '__main__': q = Que……继续阅读 » 水墨上仙 4年前 (2021-03-22) 1555浏览 1629个赞
下面的python代码根据进程的名字调用windows的taskkill命令杀死指定的进程import os command = 'taskkill /F /IM QQ.exe' #比如杀死QQ进程os.system(command) ……继续阅读 » 水墨上仙 4年前 (2021-03-22) 2418浏览 1620个赞
这个python代码创建了多个process子进程,创建完成后先start(),最后统一join,这样所有子进程会并行执行。from multiprocessing import Processimport sys, osimport timedef timetask(times): time.sleep(times) prin……继续阅读 » 水墨上仙 4年前 (2021-03-22) 1676浏览 942个赞