Time of Update: 2016-06-16
與其它大多數語言一樣,Python 也擁有 for 迴圈。你到現在還未曾看到它們的唯一原因就是,Python 在其它太多的方面表現出色,通常你不需要它們。其它大多數語言沒有像 Python 一樣的強大的 list 資料類型,所以你需要親自做很多事情,指定開始,結束和步長,來定義一定範圍的整數或字元或其它可重複的實體。但是在 Python 中,for 迴圈簡單地在一個列表上迴圈,與 list 解析的工作方式相同。1. for 迴圈介紹複製代碼 代碼如下:>>> li = ['a', 'b', '
Time of Update: 2016-06-16
python版本:複製代碼 代碼如下:#!/usr/bin/env pythonimport os, sys;def walk(path): print "cd directory:"+path for item in os.listdir(path): try: if(item == ".DS_Store"): global count count = count+1 print " find file .Ds_Store"
Time of Update: 2016-06-16
urllib基礎模組的應用,通過該類擷取到url中的html文檔資訊,內部可以重寫代理的擷取方法複製代碼 代碼如下:class ProxyScrapy(object): def __init__(self): self.proxy_robot = ProxyRobot() self.current_proxy = None self.cookie = cookielib.CookieJar() def
Time of Update: 2016-06-16
首先需要安裝chardet庫,有很多方式,我才用的是比較笨的方式:sudo pip install chardet複製代碼 代碼如下:#!/usr/bin/env python# coding: UTF-8import sysimport osimport chardet def print_usage(): print '''usage: change_charset [file|directory] [charset] [output file]\n for example:
Time of Update: 2016-06-16
複製代碼 代碼如下:import reimport urllib2import cookielibdef renren(): cj = cookielib.LWPCookieJar() opener = urllib2.build_opener(urllib2.HTTPCookieProcessor(cj)) email = '' pwd = '' #登入.. print 'login......' url = "http://www.renren.
Time of Update: 2016-06-16
python具體強大的庫檔案,很多功能都有相應的庫檔案,所以很有必要進行學習一下,其中有一個ftp相應的庫檔案ftplib,我們只需要其中的登入功能,然後利用多線程調用相應字典裡面的欄位進行登入,還能根據自己的需要,根據自身的情況編寫需要的程式,讓程式代替我們去做一些枯燥的重複工作。下面直接上代碼,下面是主檔案複製代碼 代碼如下:import os import time import threading class mythread(threading.Thread): def __init_
Time of Update: 2016-06-16
1. 最簡單的方法是用base64:複製代碼 代碼如下:import base64s1 = base64.encodestring('hello world')s2 = base64.decodestring(s1)print s1,s2# aGVsbG8gd29ybGQ=\n# hello worldNote: 這是最簡單的方法了,但是不夠保險,因為如果別人拿到你的密文,也可以自己解密來得到明文2. 第二種方法是使用win32com.client複製代碼 代碼如下:import
Time of Update: 2016-06-16
複製代碼 代碼如下:import httplibimport osimport timedef check_http(i): try: conn=httplib.HTTPConnection(i, 80, timeout=2) conn.request("GET","/") response = conn.getresponse() except Exception as e: print "server "+i+" is
Time of Update: 2016-06-16
複製代碼 代碼如下:#!/usr/bin/env pythonimport sysfrom PyQt4 import QtGui,QtCoreimport httplibfrom urllib import urlencodeimport redef out(text): p = re.compile(r'","') m = p.split(text) result=unicode(m[0][4:].decode('utf-8'))
Time of Update: 2016-06-16
python最近的工作主要是組件相容性測試,原有的架構有很多功能還不完善,需要補充!比如,需要將AutoIt指令碼的執行結果寫入到Excel中,最後的解決方案是使用本地的log來解析這個結果!增加了如下一個類來完成上述功能:複製代碼 代碼如下:class AutoItResultParser(): def ParseResult(self, vm_result, log_file): for case_result in vm_result.cases_results:
Time of Update: 2016-06-16
複製代碼 代碼如下:#!/usr/bin/env python3# -*- coding: utf-8 -*-# File Name : gt1.py# Purpose :# Creation Date : 1390366260# Last Modified : Wed 22 Jan 2014 06:14:11 PM CST# Release By : Doom.zhouimport urllib.requestimport systyp = sys.getfilesystemencoding(
Time of Update: 2016-06-16
複製代碼 代碼如下:#! /bin/bashfiles_url=(https://pypi.python.org/packages/source/s/setuptools/setuptools-2.1.tar.gz#md5=2044725530450d0517393882dc4b7508 https://pypi.python.org/packages/source/p/pip/pip-1.5.tar.gz)files=(setuptools
Time of Update: 2016-06-16
複製代碼 代碼如下:import sysimport urllibfrom urllib import requestimport osfrom bs4 import BeautifulSoupclass DramaItem: def __init__(self, num, title, url): self.num = num self.title = title self.url = url def __str__(self):
Time of Update: 2016-06-16
複製代碼 代碼如下:#!/usr/bin/env python# -*- coding:utf-8 -*-#匯入random和string模組import random, stringdef GenPassword(length): #隨機出數位個數 numOfNum = random.randint(1,length-1) numOfLetter = length - numOfNum #選中numOfNum個數字 slcNum = [random.choice(
Time of Update: 2016-06-16
需要使用到的檔案wxapp.py, read_file.py, setup.py複製代碼 代碼如下:#!/usr/bin/env python# -*- coding: utf-8 -*-#file: wxapp.pyimport wximport osimport sysimport read_fileclass Frame(wx.Frame): def __init__(self): wx.Frame.__init__(self, parent=None, title='
Time of Update: 2016-06-16
看了下函數本身的doc複製代碼 代碼如下:getattr(object, name[, default]) -> valueGet a named attribute from an object; getattr(x, 'y') is equivalent to x.y. When a default argument is given, it is returned when the attribute doesn't exist; without it, an exception is
Time of Update: 2016-06-16
複製代碼 代碼如下:#!/usr/bin/env python#coding:utf-8import re,sysimport urllibfrom bs4 import BeautifulSoupglobal r_urldef hq_url(): so_url = "http://movie.douban.com/subject_search?search_text=" data = urllib.urlopen(so_url+gjz).read() r =
Time of Update: 2016-06-16
複製代碼 代碼如下:#! /usr/bin/env python#coding=utf-8from __future__ import unicode_literalsfrom multiprocessing.dummy import Pool as ThreadPoolimport threadingimport osimport sysimport cPicklefrom collections import namedtupleimport urllib2from urlparse
Time of Update: 2016-06-16
第一步,建立一個CPP的DLL工程,然後寫如下代碼,產生DLL複製代碼 代碼如下:#include #define DLLEXPORT extern "C" __declspec(dllexport) DLLEXPORT int __stdcall hello() { printf("Hello world!\n"); return 0; } 第二步,編寫一個 python 檔案:複製代碼 代碼如下:# coding: utf-8
Time of Update: 2016-06-16
說明1. 使用google翻譯服務獲得翻譯和語音;2. 使用mplayer播放獲得的音效檔,因此,如果要播放語音,請確保PATH中能夠找到mplayer程式,如果沒有mplayer,請將use_tts設定為False運行。即:main(use_tts=False)3. 退出程式,輸入"x",斷行符號。複製代碼 代碼如下:#! /usr/bin/env python#coding=utf-8import requestsdef translate(words): import re