【python – PyQt:获取当前标签号】教程文章相关的互联网学习教程文章

python实现获取序列中最小的几个元素

本文实例讲述了python实现获取序列中最小的几个元素。分享给大家供大家参考。 具体方法如下:import heapq import random def issorted(data): data = list(data) heapq.heapify(data) while data: yield heapq.heappop(data) alist = [x for x in range(10)] random.shuffle(alist) print the origin list is,alist print the min in the list is for x in issorted(alist): print x,程序运行结果如下:the origin list is ...

python实现批量获取指定文件夹下的所有文件的厂商信息

本文实例讲述了python实现批量获取指定文件夹下的所有文件的厂商信息的方法。分享给大家供大家参考。具体如下: 功能代码如下:import os, string, shutil,re import pefile import codecs, sys import wx import struct #输出中打印Unicode字符 #sys.stdout = codecs.lookup(utf-8)[-1](sys.stdout) def addToDict(theDict,PEfile_Path,strCompanyName): theDict.setdefault(PEfile_Path, [ ]).append(strCompanyName)#存在...

python获取Linux下文件版本信息、公司名和产品名的方法

本文实例讲述了python获取Linux下文件版本信息、公司名和产品名的方法,分享给大家供大家参考。具体如下: 区别于前文所述。本例是在linux下得到文件版本信息,主要是通过pefile模块解析文件 中的字符串得到的。代码如下:def _get_company_and_product(self, file_path): """ Read all properties of the given file return them as a dictionary. @return: a tumple, (company, product) """ mype = pefile.PE(file_path) compan...

python获取文件版本信息、公司名和产品名的方法

本文实例讲述了python获取文件版本信息、公司名和产品名的方法,分享给大家供大家参考。具体如下: 该python代码可得到文件版本信息、公司名和产品名。其他的信息都在返回的字典中。具体代码如下:def _getCompanyNameAndProductName(self, file_path): """ Read all properties of the given file return them as a dictionary. """ propNames = (Comments, InternalName, ProductName, CompanyName, LegalCopyright, ProductVersi...

Python获取Linux系统下的本机IP地址代码分享

有时候使用到获取本机IP,就采用以下方式进行。代码如下: #!/usr/bin/python import socket import struct import fcntl def getip(ethname): s=socket.socket(socket.AF_INET, socket.SOCK_DGRAM) return socket.inet_ntoa(fcntl.ioctl(s.fileno(), 0X8915, struct.pack(‘256s, ethname[:15]))[20:24]) if __name__==__main__: print getip(eth0)

Python中获取网页状态码的两个方法

第一种是用urllib模块,下面是例示代码:代码如下: import urllib status=urllib.urlopen("http://www.gxlcms.com").code print status第二章是用requests模块,下面是例示代码:代码如下: import requests code=requests.get("http://www.gxlcms.com").status_code print code

python获取文件后缀名及批量更新目录下文件后缀名的方法

本文实例讲述了python获取文件后缀名及批量更新目录下文件后缀名的方法。分享给大家供大家参考。具体实现方法如下: 1. 获取文件后缀名:代码如下:#!/usr/bin/python import os dict = {} for d, fd, fl in os.walk(/home/ahda/Program/):for f in fl:sufix = os.path.splitext(f)[1][1:]if dict.has_key(sufix):dict[sufix] += 1else:dict[sufix] = 1 for item in dict.items():print "%s : %s" % item 这里的关键是os.path.split...

Python获取Windows或Linux主机名称通用函数分享

通过python的os模块获取windows或者linux主机名的通用函数。代码如下: #!/usr/bin/env python #coding=utf-8 import os def hostname(): sys = os.name if sys == nt: hostname = os.getenv(computername) return hostname elif sys == posix: host = os.popen(echo $HOSTNAME) try: hostname = host.read() return hostname finally: host.close() else: return Unkwon hostname

Linux下Python获取IP地址的代码

《lnmp一键安装包》中需要获取ip地址,有2种情况:如果服务器只有私网地址没有公网地址,这个时候获取的IP(即私网地址)不能用来判断服务器的位置,于是取其网关地址用来判断服务器在国内还是国外(脚本为了使国内用户快速下载,yum源自动设置成163,这个情况就需要获取网关地址);如果服务器有公网地址,这时获取的IP地址可用来直接判断服务器地理位置。 获取服务器IP,如果有公网地址就取公网地址,没有公网地址就取私网网址 下...

Python实现获取某天是某个月中的第几周

找了半天竟然没找到,如何在Python的datetime处理上,获取某年某月某日,是属于这个月的第几周。 无奈之下求助同学,同学给写了一个模块。【如果你知道Python有这个原生的库,请不吝赐教】 我稍作整理记录在下。代码如下: #!/usr/bin/env python # -*- coding: utf-8 -*- __author__ = #### import datetime def get_week_of_month(year, month, day):"""获取指定的某天是某个月中的第几周周一作为一周的开始"""end = int(datetime...

Python可跨平台实现获取按键的方法

本文实例讲述了Python可跨平台实现获取按键的方法。分享给大家供大家参考。具体如下:代码如下:class _Getch: """Gets a single character from standard input. Does not echo to the screen.""" def __init__(self): try: self.impl = _GetchWindows() except ImportError: try: self.impl = _GetchMacCarbon() except AttributeError: self.impl = _GetchUnix() def __call__(self): return self.impl() class _Get...

Python获取DLL和EXE文件版本号的方法

本文实例讲述了Python获取DLL和EXE文件版本号的方法。分享给大家供大家参考。具体实现方法如下:代码如下:import win32api def getFileVersion(file_name):info = win32api.GetFileVersionInfo(file_name, os.sep)ms = info[FileVersionMS]ls = info[FileVersionLS]version = %d.%d.%d.%04d % (win32api.HIWORD(ms), win32api.LOWORD(ms), win32api.HIWORD(ls), win32api.LOWORD(ls))return version 希望本文所述对大家的Python程序...

Python实现从百度API获取天气的方法

本文实例讲述了Python实现从百度API获取天气的方法。分享给大家供大家参考。具体实现方法如下:代码如下:__author__ = saint import os import urllib.request import urllib.parse import json class weather(object):# 获取城市代码的uricode_uri = "http://apistore.baidu.com/microservice/cityinfo?cityname="# 获取天气信息的uriweather_uri = "http://apistore.baidu.com/microservice/weather?cityid="# 主处理逻辑def mai...

python通过urllib2获取带有中文参数url内容的方法

本文实例讲述了python通过urllib2获取带有中文参数url内容的方法。分享给大家供大家参考。具体如下: 对于中文的参数如果不进行编码的话,python的urllib2直接处理会报错,我们可以先将中文转换成utf-8编码,然后使用urllib2.quote方法对参数进行url编码后传递。content = u你好 jb51.net content = content.encode(utf-8) content = urllib2.quote(content) api_url = http://www.gxlcms.com/q=%s%content res = urllib2.urlopen(...

Python获取单个程序CPU使用情况趋势图【图】

本文定位:已将CPU历史数据存盘,等待可视化进行分析,可暂时没有思路。 前面一篇文章(http://www.gxlcms.com/article/61956.htm)提到过在linux下如何用python将top命令的结果进行存盘,本文是它的后续。 python中我们可以用matplotlib很方便的将数据可视化,比如下面的代码:代码如下: import matplotlib.pyplot as plt list1 = [1,2,3] list2 = [4,5,9] plt.plot(list1,list2) plt.show()执行效果如下:上面只是给plot函数传了...