Category: Python
-
python查看已经安装的模块
方法一: >>> help(‘modules’) Please wait a moment while I gather a list of all available modules… …. Enter any module name to get more help. Or, type “modules spam” to search for modules whose descriptions contain the word “spam”. 方法二: >>> import sys >>> sys.modules
-
ip2long与long2ip
在php中可以知道 ip2long 函数将ip转为整型,long2ip将整型转为ip,从而方便计算。 ip分为四段,将第一段*256^3,第二段*256^2,第三段*256^1,第四段*256^0,最后相加,这样就计算出了一个唯一的值。如果都是最大值,算出来的值是 >>> 255*256*256*256 + 255*256*256 + 255*256 + 255 4294967295 我们可以发现mysql中int型的取值范围是4个字节,十进制为 0 到 4294967295 所以所有的ipv4地址用mysql的int恰好可以完整记录 python中互相转换的方法如下 import socket import struct def ip2long(ipstr): return struct.unpack(“!I”, socket.inet_aton(ipstr))[0] def long2ip(ip): return socket.inet_ntoa(struct.pack(“!I”, ip)) http://hily.me/blog/2009/03/python-ip2long-long2ip/
-
整理python的csv.reader和csv.writer
以下脚本是读取以空格分开的字段(weight.txt)变成以逗号分隔(weight.csv) #!/usr/bin/env python #-*- coding:utf-8 -*- # author jpuyy.com import csv reader = csv.reader(open(‘weight.txt’, ‘rb’), delimiter=’ ‘, quoting=csv.QUOTE_NONE) writer = csv.writer(open(‘weight.csv’, ‘wb’)) for row in reader: writer.writerow(row) 解决 csv 用 excel 打开乱码问题 import codecs file_obj = open(csv_name, “wb+”) file_obj.write(codecs.BOM_UTF8) f = csv.writer(file_obj)
-
Ubuntu下安装Django
Django为流行的python web开发框架 安装python的包管理器,easy_install # apt-get install python-setuptools 可以使用下面的命令检查是否安装成功 # easy_install –version 显示版本信息则安装成功 接下来使用easy_install安装Django # easy_install django 使用如下命令查看django是否安装成功 # python Python 2.7.3 (default, Aug 1 2012, 05:14:39) [GCC 4.6.3] on linux2 Type “help”, “copyright”, “credits” or “license” for more information. >>> import django >>> django.VERSION (1, 4, 3, ‘final’, 0) 出现版本信息,则表示安装成功。
-
python的入门的几个例子(下)
http://jpuyy.com/2012/12/python-simple-programs-a.html http://jpuyy.com/2013/01/python-simple-programs-b.html 例.20 素数筛选,生成器–Prime numbers sieve w/fancy generators 生成从2到1000以内的素数 import itertools def iter_primes(): # an iterator of all numbers between 2 and +infinity numbers = itertools.count(2) # generate primes forever while True: # get the first number from the iterator (always a prime) prime = numbers.next() yield prime # this code iteratively builds up a…
-
python的入门的几个例子(中)
接着上一个http://jpuyy.com/2012/12/python-simple-programs-a.html 例11.三引号,while循环–Triple-quoted strings, while loop REFRAIN = ”’ %d bottles of beer on the wall, %d bottles of beer, take one down, pass it around, %d bottles of beer on the wall! ”’ bottles_of_beer = 99 while bottles_of_beer > 1: print REFRAIN % (bottles_of_beer, bottles_of_beer, bottles_of_beer – 1) bottles_of_beer -= 1 例12.python中的类–Classes class BankAccount(object): def…