python小程序

一、介绍

学习过程中的小程序

汉诺塔

def hano(n, A, B, C):
    if n==1:
        print ("第 %s 块 从 %s ---> %s" % (n, A, C))
    else:
        hano(n-1, A, C,B)
        print ("第 %s 快 从 %s ---> %s" % (n, A, C))
        hano(n-1, B, A, C)
hano(3,'A','B','C')

Python正则表达式之抓取网页练习

import re
from urllib import *

def GetPic():
    global listurl
    url = 'http://www.imooc.com/course/list'
    req = urlopen(url)
    buf = req.read()
    buf =  buf.decode('utf-8')
    listurl = re.findall(r'img\d.+?\.jpg',buf)  #非贪婪匹配。
    listurl = list(set(listurl)) #去除掉重复的元素
    def Save():
    index = 0
    for url in listurl:
        f = open(str(index)+'.jpg','w')  
#有则打开,无则新建一个图片文件!!并且以二进制方式打开,并可以写入。
        req = urlopen("http://"+url)     
        buf = req.read()
        f.write(str(buf)) 
        index += 1    

if __name__ == '__main__':
    GetPic()    
    Save()

@爬取的图片都已经损坏了,无法打开。

猜数字游戏

#猜数字游
import random 
number=random.randint(1,20)
guessesTaken=0
while guessesTaken<6:
    print ('请输入你猜测的数字,你还有'+str(6-guessesTaken)+'次机会!')
    guess=int(input())
    if number==guess :
        print ('恭喜你,猜对了!')
        break
    elif number>guess : 
            print ('猜小了')
    else :
            print('猜大了')
    guessesTaken+=1

随机选取人员代码

#随机选取人员代码
import os
from numpy import *
import random

def openFile(): #打开文件 
    global numLines #行数
    global headLine #保存标题
    global persons #保存人员信息
    global isSelected #是否已经选择过了 
    fileName = 'person.txt'
    fr = open(fileName)#打开文件
    rows = fr.readlines()#按行读取,返回值为列表
    numLines = len(rows)-1
    headLine = []    
    persons = []    
    isSelected = [] 
    firstline = True  #是否是标题行
    #初始化选择的属性,是否已经被选择了
    for i in range(numLines):
        isSelected.append(False)

    #如何保存多个属性,这里用的是列表,也可以用字典来进行保存,矩阵只能保持数字
    for line in rows:
        line = line.strip('\n')
        if firstline:
            headLine = line.split('\t')
            firstline = False
        else:
            line = line.split('\t')
            persons.append(line)

#def randOnePerson():


def randAllPerson():
    #print(headLine)

    index = 1  #序号
    #显示标题
    for i in range(len(headLine)):
            print('\t' + headLine[i] + '\t', end='')
    print()
    while True:
        num = random.randint(1,numLines)

        if index == numLines:
            for i in range(numLines-1):
                if isSelected[i] == False:
                    print('第 %d 个是,%s\t%s\t%s\t%s\t%s\t' 
                  % (index, persons[i][0],
                     persons[i][1],
                     persons[i][2],
                     persons[i][3],
                     persons[i][4]))
                    return

        if not isSelected[num-1]:
            print('第 %d 个是,%s\t%s\t%s\t%s\t%s\t' 
                  % (index, persons[num-1][0],
                     persons[num-1][1],
                     persons[num-1][2],
                     persons[num-1][3],
                     persons[num-1][4]))
            isSelected[num-1] = True
            index +=1

if __name__ == '__main__':
    openFile()
    randAllPerson()

我们谈到“文本处理”时,我们通常是指处理的内容。Python 将文本文件的内容读入可以操作的字符串变量非常容易。文件对象提供了三个“读”方法: .read()、.readline() 和 .readlines()。每种方法可以接受一个变量以限制每次读取的数据量,但它们通常不使用变量。 .read() 每次读取整个文件,它通常用于将文件内容放到一个字符串变量中。然而 .read() 生成文件内容最直接的字符串表示,但对于连续的面向行的处理,它却是不必要的,并且如果文件大于可用内存,则不可能实现这种处理。
.readline() 和 .readlines() 非常相似。它们都在类似于以下的结构中使用:

Python .readlines() 示例
fh = open(‘c:\autoexec.bat’)
for line in fh.readlines():
print line
.readline() 和 .readlines() 之间的差异是后者一次读取整个文件,象 .read() 一样。.readlines() 自动将文件内容分析成一个行的列表,该列表可以由 Python 的 for … in … 结构进行处理。另一方面,.readline() 每次只读取一行,通常比 .readlines() 慢得多。仅当没有足够内存可以一次读取整个文件时,才应该使用 .readline()。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值