joke python

w

# -*- coding: utf-8 -*-
import pycurl
import re
import cStringIO
from pypinyin import lazy_pinyin

import sys, locale

buf = cStringIO.StringIO()

GRAB_URL = ‘http://www.gov.cn/test/2011-08/22/content_1930111.htm‘

c = pycurl.Curl()
c.setopt(c.HTTPHEADER, [‘Accept-Charset: UTF-8‘])
c.setopt(c.URL, GRAB_URL)
c.setopt(c.WRITEFUNCTION, buf.write)
c.perform()

grab_data = buf.getvalue().decode(‘utf-8‘)
tmp_list = re.findall(r"\d{3}[^0]0{2}.*<", grab_data)
# 词尾一样
shi_dic = {}
# 词头一样
shi_dic_head = {}
for i in tmp_list:
    tmp = i.split(‘ ‘)
    city = tmp[1].split(‘<‘)
    try:
        shi_exist = city[0].index(u‘市‘)
        if shi_exist > 1:
            # 深圳市
            shi_tail = city[0].split(u‘市‘)
            # 深圳
            shi = shi_tail[0]
            shi_pinyin = lazy_pinyin(shi)
            tail_pinyin = shi_pinyin[-1]
            head_pinyin = shi_pinyin[0]
            if shi_dic.has_key(tail_pinyin):
                shi_dic[tail_pinyin].append(shi)
            else:
                shi_dic[tail_pinyin] = [shi]

            if shi_dic_head.has_key(head_pinyin):
                shi_dic_head[head_pinyin].append(shi)
            else:
                shi_dic_head[head_pinyin] = [shi]
        else:
            continue
    except Exception, e:
        # print Exception, ":", e
        continue

def letsgo():
    info = raw_input(u‘请输入,如“深圳”:‘.encode(‘utf-8‘)).decode(sys.stdin.encoding or locale.getpreferredencoding(True))

    info_pinyin = lazy_pinyin(info)
    info_shi_pinyin = info_pinyin[-1]
    flag = 1
    # if shi_dic.has_key(info_shi_pinyin):
    #     flag = 0
    #     for i in shi_dic[info_shi_pinyin]:
    #         print i
    if shi_dic_head.has_key(info_shi_pinyin):
        flag = 0
        for i in shi_dic_head[info_shi_pinyin]:
            print i
    if flag == 1:
        print "输入字符串非法"
    die = raw_input(u‘如继续,请输入go,如结束请输入其他‘)
    if die == ‘go‘:
        letsgo()
    else:
        print ‘程序已经退出‘

letsgo()

  

时间: 2024-08-10 00:07:10

joke python的相关文章

python练习题-20170916

周末开始第一次尝试对着书写代码练习题 <笨办法学python>--作者Zed A.Shaw,翻译Wang Dingwei ex1.pyprint('hello world')---------------------------------------ex2.py #A comment, this is so you can read your program later.#Anything after the # is ignored by python. print('i could ha

笨办法学Python(六)

习题 6: 字符串(string)和文本 虽然你已经在程序中写过字符串了,你还没学过它们的用处.在这章习题中我们将使用复杂的字符串来建立一系列的变量,从中你将学到它们的用途.首先我们解释一下字符串是什么东西.    字符串通常是指你想要展示给别人的.或者是你想要从程序里"导出"的一小段字符.Python 可以通过文本里的双引号 " 或者单引号 ' 识别出字符串来.这在你以前的 print 练习中你已经见过很多次了.如果你把单引号或者双引号括起来的文本放到 print 后面,它

笨办法学Python (exercise1-15)

#exercise1print "Hello world!"print "Hello Again"print "I like typing this."print "this is fun."print 'Yay! Printing.'print "I'd much rather you 'not'."print 'I "said" do not touch this.' #exerci

Python的字典和集合

字典 字典是Python种唯一的映射类型.映射类型中的数据是无序排列的.在映射类型种,我们使用键直接‘映射’到值,这就是为什么叫映射类型的原因,映射类型通常被称做哈希表,因为字典对象就是哈希类型的.哈希表的算法是获取键,对键执行一个叫做哈希函数的操作,并根据计算的结果,选择在数据结构的某个地址中来存储值.任何一个值存储的地址取决于它的键,因此,哈希表中的值是没有顺序的.哈希表有一个很好的性能,因为用键查询相当快. 字典的基本操作 #字典的赋值 >>> dict1 = {} >>

一入Python深似海--print

先给大家来个干货^~^,学习Python的一个好站点,http://learnpythonthehardway.org/book/ 经典样例 以下是几个老经典的样例喽,刚接触Python的能够敲一敲,看看结果喽! my_name='Zed A. Shaw' my_age=35#not a lie my_height=74#inches my_weight=180#1bs my_eyes='Blue' my_teeth='white' my_hair='Brown' print my_name p

python部分内容存档

笨办法学python. 1 Ec6字符串和文本... 1 ec7. 1 ec8. 1 Ec9. 1 Ec10 转义字符... 1 Ec11提问... 1 raw_input和input的区别... 1 Ec12提示别人... 1 ec13 参数,解包,变量... 1 ec14提示和传递... 1 Ec15读取文件... 1 Ec16读写文件... 1 ec17更多文件操作... 1 ec18命名,变量,代码,函数... 1 ec19函数和变量... 1 ec20函数和文件... 1 ec21函数

python编程之函数思想

#!/usr/bin/python ##Filename: game.py from sys import exit from random import randint def death():   quips = ["1-dead", "2-nice", "3-luser", "4-puppy"]   print quips[randint(0,len(quips)-1)]   exit(1) #death() def c

Python写各大聊天系统的屏蔽脏话功能原理

Python写各大聊天系统的屏蔽脏话功能原理 突然想到一个视频里面弹幕被和谐的一满屏的*号觉得很有趣,然后就想用python来试试写写看,结果还真玩出了点效果,思路是首先你得有一个脏话存放的仓库好到时候检测,那么个人还是喜欢用列表,因为列表灵活使用扩展都很方便,有了脏话库我们在来想核心怎么屏蔽脏话,你要记得玩家输入的弹幕数据类型是什么首档其次是字符串如果没有特殊要求就它了,万变不离其中总是这几个数据结构嘛,有了字符串替换*号什么的都轻松许多了对吧,今天所聊的是完整的一套结构,为了让大家更清晰学会

python--笨方法学python 习题52

笨方法学python是一本不错的python入门书籍.书的最后一节是一个web版的游戏程序,以下是程序代码: 1.项目的目录结构如下所示:skeleton\ app.py map.py templates\ layout.html show_room.html you_died.html 2.游戏地图结构 map.py其中Room类是游戏场景,包括场景名称 name,描述 description,通往下一场景的可能路径集合 paths. 定义了游戏中设计到的7个场景. # -*- coding: