python 中文件路径和url的相互转换import urllib pathname = 'path/to/file/or/folder/' url = urllib.pathname2url(pathname) pathname = urllib.url2pathname(url)……继续阅读 » 水墨上仙 6年前 (2021-01-15) 4096浏览 330个赞
python将字符串转换成单词首字母大写的标题格式 方法1a = 'hello world! how are you?' b = ' '.join(i.capitalize() for i in a.split(' '……继续阅读 » 水墨上仙 6年前 (2021-01-15) 2342浏览 670个赞
python计算指定字符串在另一个字符串中出现的次数s = "Count, the number,, of commas." print s.count(",") 输出3……继续阅读 » 水墨上仙 6年前 (2021-01-15) 3194浏览 1056个赞
python创建目录并更改权限import os os.mkdir("foo", 0666) ……继续阅读 » 水墨上仙 6年前 (2021-01-15) 2389浏览 704个赞
python计算文本文件的行数filename = "somefile.txt" myfile = open(filename) lines = len(myfile.readlines()) print "There are %d lines in %s" % (lines, filename) ……继续阅读 » 水墨上仙 6年前 (2021-01-15) 3395浏览 560个赞
python转换字符串为摩尔斯电码chars = ",.0123456789?abcdefghijklmnopqrstuvwxyz"codes = """--..-- .-.-.- ----- .---- ..--- ...-- ....- ..... -.... --... ---.. ……继续阅读 » 水墨上仙 6年前 (2021-01-15) 3141浏览 421个赞
python文件拷贝代码,这段代码演示了通过文件名拷贝和通过文件对象拷贝import shutil # copy files by name shutil.copyfile('/path/to/file', '/path/to/other/phile') # copy file-objec……继续阅读 » 水墨上仙 6年前 (2021-01-15) 3226浏览 891个赞
python获得文件大小的代码TheFileSize = os.path.getsize(TheFileName) ……继续阅读 » 水墨上仙 6年前 (2021-01-15) 2722浏览 2097个赞
python字符串和整形的相互转换print int("42"), str(42) # convert from/to stringprint int("42") + 1 # force addition……继续阅读 » 水墨上仙 6年前 (2021-01-15) 3552浏览 2797个赞
python获取数组元素的个数mySeq = [1,2,3,4,5] len(mySeq) ……继续阅读 » 水墨上仙 6年前 (2021-01-15) 2402浏览 2626个赞
python更新列表(数组)aList = [123, 'abc', 4.56, ['inner', 'list'], (7-9j)]print aList[2]aList[2] = 'float replacer'print aListaList.append(&……继续阅读 » 水墨上仙 6年前 (2021-01-15) 2520浏览 2934个赞
遗传算法的神经网络python实现代码## {{{ http://code.activestate.com/recipes/578241/ (r1)from operator import itemgetter, attrgetterimport mathimport randomimport stringimport timeitfrom……继续阅读 » 水墨上仙 6年前 (2021-01-15) 1579浏览 1010个赞
python列表操作:追加元素到列表scores = ["1","2","3"]# add a scorescore = int(raw_input("What score did you get?: "))scores.append(score)# list hi……继续阅读 » 水墨上仙 6年前 (2021-01-15) 3324浏览 611个赞
python列表操作extend和append的区别演示代码li = ['a', 'b', 'c'] li.extend(['d', 'e', 'f']) print li pri……继续阅读 » 水墨上仙 6年前 (2021-01-15) 4042浏览 2706个赞
python检查字符串是否是正确的ISBNdef isISBN(isbn): """Checks if the passed string is a valid ISBN number.""" if len(isbn) != 10 or not isbn[:9].isd……继续阅读 » 水墨上仙 6年前 (2021-01-15) 2959浏览 1832个赞
如果是以指定的字符串结束则返回Truename = raw_input('What is your name? ')if name.endswith('Yin'): print 'Hello, Mr. Yin'……继续阅读 » 水墨上仙 6年前 (2021-01-15) 3039浏览 2946个赞
python检查指定的文件是否存在import os def file_exists(file_name): if os.path.exists(file): return '%s is found' % file_name else: return ……继续阅读 » 水墨上仙 6年前 (2021-01-15) 3146浏览 1310个赞
python支持两个注释方法,#和三个单引号,#用于单行注释,三个单引号用于多行注释# single line comment '''''Multi-line string. Not a comment in itself. But it is usually used as the firs……继续阅读 » 水墨上仙 6年前 (2021-01-15) 2443浏览 2376个赞
python 列表操作:插入元素到列表li = ['a', 'b', 'mpilgrim', 'z', 'example'] li.insert(2, "new") p……继续阅读 » 水墨上仙 6年前 (2021-01-15) 3681浏览 2799个赞
python在字符串中查找子字符串,如果找到则返回子字符串的位置,如果没有找到则返回-1S = 'xxxxSPAMxxxxSPAMxxxx'where = S.find('SPAM') # search for positionprint where ……继续阅读 » 水墨上仙 6年前 (2021-01-15) 3166浏览 2646个赞
python字符串对其居中的演示代码,下面的代码可以让字符串居中,左对齐和右对齐,字符串长度设置为50,居中后左右补充空格,右对齐会在左侧补充空格string1 = "Now I am here."print string1.center( 50 )print string1.rjust( 50 )print string1.……继续阅读 » 水墨上仙 6年前 (2021-01-15) 1761浏览 1283个赞
python列表连接添加性能测试代码L = [1, 2]L = L + [3] # concatenate: slowerprint LL.append(4) # faster, but in-placeprint LL = L + [5, 6] # concatenate: slo……继续阅读 » 水墨上仙 6年前 (2021-01-15) 3258浏览 2239个赞
python创建列表并给列表赋初始值演示代码aList = [123, 'abc', 4.56, ['inner', 'list'], 7-9j]anotherList = [None, 'something to see here']print aListprint……继续阅读 » 水墨上仙 6年前 (2021-01-15) 1771浏览 399个赞
python for each遍历字典D = {'a':1, 'b':2, 'c':3}for key in D: print key, D[key]……继续阅读 » 水墨上仙 6年前 (2021-01-15) 2624浏览 2604个赞
python提取字典的key列表,这段代码可以把字典的所有key输出为一个数组d2 = {'spam': 2, 'ham': 1, 'eggs': 3} # make a dictionaryprint d2 ……继续阅读 » 水墨上仙 6年前 (2021-01-15) 3362浏览 2002个赞
python获取指定路径下所有指定后缀的文件# 获取指定路径下所有指定后缀的文件# dir 指定路径# ext 指定后缀,链表&不需要带点 或者不指定。例子:['xml', 'java']def GetFileFromThisRootDir(dir,ext = None): allfiles……继续阅读 » 水墨上仙 6年前 (2021-01-15) 2528浏览 2241个赞
python只在需要的时候应用装饰排序 def lazyDSU_sort(seq, keys, warn=False): """Return sorted seq, breaking ties by applying keys only when needed. If ``warn``……继续阅读 » 水墨上仙 6年前 (2021-01-15) 2022浏览 758个赞
python通过进程内通信实现的聊天室程序代码#!/usr/bin/env python# Added by <ctang@redhat.com>import sysimport osfrom multiprocessing import connectionADDR = ('', 9997)AUTH_KEY……继续阅读 » 水墨上仙 6年前 (2021-01-15) 2273浏览 1058个赞
coreseek自带了mssql的python数据源演示,我简单的修改了一下,用于mysql测试通过# -*- coding:utf-8 -*-# coreseek3.2 4.1 python source演示操作mysql数据库# author: 75271.com# date: 2012-09-13 10:20from os import ……继续阅读 » 水墨上仙 6年前 (2021-01-15) 2827浏览 2966个赞
python将英文单词表示的数字转换成阿拉伯数字import re_known = { 'zero': 0, 'one': 1, 'two': 2, 'three': 3, 'four': 4, ……继续阅读 » 水墨上仙 6年前 (2021-01-15) 2540浏览 396个赞
python装饰器,用来计算指定函数的运行时间def GetRunTime(func): def check(*args, **args2): startTime = datetime.datetime.now() f = func(*args,**args2) endTime = dateti……继续阅读 » 水墨上仙 6年前 (2021-01-15) 1773浏览 2844个赞
python中发送邮件的代码片段 来源:http://www.cnblogs.com/xiaowuyi/archive/2012/03/17/2404015.html python中email模块使得处理邮件变得比较简单,今天着重学习了一下发送邮件的具体做法,这里写写……继续阅读 » 水墨上仙 6年前 (2021-01-15) 3281浏览 2960个赞
开启用户验证下的gridfs 连接使用,在执行脚本前可以在python shell中from pymongo import Connectionfrom gridfs import *con = Connection(“mongodb://admin:admin@127.0.0.1:27017”)#用URI的方式建立数据库的链接,当然……继续阅读 » 水墨上仙 6年前 (2021-01-15) 3554浏览 1297个赞
python获得文件的创建时间和修改时间并输出的代码,需要用户从控制台输入文件路径import os.path, timeimport exceptionsclass TypeError (Exception): passif __name__ == '__main__': if (len(os.sys.argv)……继续阅读 » 水墨上仙 6年前 (2021-01-15) 2883浏览 2908个赞
bootstrapping and forward curve生成python范例## Description: example of a bootstrapping and forward curve generation# script, this can be used to build a set of curves for differ……继续阅读 » 水墨上仙 6年前 (2021-01-15) 2320浏览 2332个赞
python正则表达式提取网页URLimport reimport urlliburl="http://www.itokit.com"s=urllib.urlopen(url).read()ss=s.replace(" ","")urls=re.findall(r"<……继续阅读 » 水墨上仙 6年前 (2021-01-15) 1761浏览 1392个赞
Windows下Python获取磁盘空闲空间并写入日志from ctypes import *import timeimport win32filerun = Truelogfile = open('.\\log.out','w+');#open log fileinput = raw_input(&q……继续阅读 » 水墨上仙 6年前 (2021-01-15) 3322浏览 633个赞
用urllib按照百度音乐分类下载mp3。#!/usr/bin/env python#-*- coding: utf-8 -*-import urllibimport rebaseurl = "http://music.baidu.com"url = "http://music.baidu.com/search/……继续阅读 » 水墨上仙 6年前 (2021-01-15) 2448浏览 1775个赞
python处理大数字代码# check the numeric range of Python with huge factorials# factorial(69) still checks out accurately! Has 98 digits in it!# 171122452428141311372468338881272839092……继续阅读 » 水墨上仙 6年前 (2021-01-15) 3384浏览 140个赞
上线需要,将py的源码中注释删掉,然后编译成字节码。写此脚本主要是为了删除注释。当然如果上线不想放py源码,则在最后增加删除源码即可。#!/bin/env python# -*- coding:utf-8 -*-# -------------------------------# Filename: # Revision:# Date:……继续阅读 » 水墨上仙 6年前 (2021-01-15) 3163浏览 2795个赞
python提取页面内的url列表from bs4 import BeautifulSoupimport time,re,urllib2t=time.time()websiteurls={}def scanpage(url): websiteurl=url t=time.time() n=0 html=……继续阅读 » 水墨上仙 6年前 (2021-01-15) 3261浏览 2059个赞
python 类继承演示范例# a simple example of a class inheritance# tested with Python24 vegaseat 10aug2005help('object') # testclass Class1(object): ""&……继续阅读 » 水墨上仙 6年前 (2021-01-15) 3061浏览 2489个赞
python统计文本字符串里面单词出现的频率# word frequency in a text# tested with Python24 vegaseat 25aug2005# Chinese wisdom ...str1 = """Man who run in front of car, get t……继续阅读 » 水墨上仙 6年前 (2021-01-15) 2563浏览 1869个赞
python正则搜索范例,通过search正则搜索字符串# encoding: UTF-8 import re # 将正则表达式编译成Pattern对象 pattern = re.compile(r'world') # 使用search()查找匹配的子串,不存在能匹配的子串时将返回None # 这个例子中使用mat……继续阅读 » 水墨上仙 6年前 (2021-01-15) 2156浏览 920个赞
python 定时执行指定的函数# time a function using time.time() and the a @ function decorator# tested with Python24 vegaseat 21aug2005import timedef print_timing(func): def wr……继续阅读 » 水墨上仙 6年前 (2021-01-15) 1897浏览 122个赞