Upload
takeshi-akutsu
View
182
Download
2
Embed Size (px)
Citation preview
In [22]: # キーが無いとエラーmy_dict['e']
---------------------------------------------------------------------------KeyError Traceback (most recent call last)<ipython-input-22-20aaaefffc13> in <module>()----> 1 my_dict['e']
KeyError: 'e'
In [27]: # キーと要素のペアにアクセスfor k,v in my_dict.items(): print('key={}\tvalue={}'.format(k,v))
key=b value=2key=a value=1key=c value=3key=d value=4
In [82]: # 初期化メソッド
class MyClass:
def __init__(self,a):
self.a = a
my_class = MyClass(10)
my_class.a
Out[82]: 10
In [83]: # インスタンスメソッド
class MyClass:
def __init__(self,a):
self.a = a
def multi(self,b):
return self.a ** b
my_class = MyClass(2)
my_class.multi(3)
Out[83]: 8
In [84]: # 順列import itertoolsfor v in itertools.permutations([1,2,3],2): print(v)
(1, 2)(1, 3)(2, 1)(2, 3)(3, 1)(3, 2)
In [89]: # 集計
import collections
c = collections.Counte
r([1,2,1,1,1,2,2,3,3,3,3])
c
Out[89]: Counter({1: 4, 3: 4, 2: 3})
In [90]: # キーが無くてもエラーにならないprint('# of 1 -> {}'.format(c[1]))print('# of 5 -> {}'.format(c[5]))
# of 1 -> 4# of 5 -> 0
In [91]: # 順番の着いた辞書型
od = collections.OrderedDict()
od['a'] = 1
od['b'] = 2
od['c'] = 3
print(od)
OrderedDict([('a', 1), ('b', 2), ('c', 3)])
In [92]: # こちらは辞書なので、キーが無いとエラーod['d']
---------------------------------------------------------------------------KeyError Traceback (most recent call last)<ipython-input-92-4e5ff27c4578> in <module>() 1 # こちらは辞書なので、キーが無いとエラー----> 2 od['d']
KeyError: 'd'
In [93]: # リーディング・エッジ社のホームページに接続import urllib.request as urc = ur.urlopen('http://www.leadinge.co.jp')html = c.readall()
In [94]: #バイト列html
Out[94]: b'<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"\n"http://www.w3.org/TR/html4/loose.dtd">\n<html>\n<head>\n<meta http-equiv="Content-Type" content="text/html; charset=utf-8">\n<meta name="description" content= "Linux\xe3\x82\xa8\xe3\x83\xb3\xe3\x82\xb8\xe3\x83\x8b\xe3\x82\xa2\xe3\x80\x81\xe3\x83\x8d\xe3\x83\x83\xe3\x83\x88\xe3\x83\xaf\xe3\x83\xbc\xe3\x82\xaf\xe3\x82\xa8\xe3\x83\xb3\xe3\x82\xb8\xe3\x83\x8b\xe3\x82\xa2\xe3\x80\x81\xe3\x82\xb5\xe3\x83\xbc\xe3\x83\x90\xe7\xae\xa1\xe7\x90\x86\xe8\x80\x85\xef\xbc\x88\xe3\x82\xb5\xe3\x83\xbc\xe3\x83\x90\xe3\x83\xbc\xe7\xa7\xbb\xe8\xa1\x8c\xe3\x82\x84\xe3\x82\xb5\xe3\x83\xbc\xe3\x83\x90\xe3\x8
In [95]: # UTF-8で文字列に変換html_str = html.decode('utf-8')html_str
Out[95]: '<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"\n"http://www.w3.org/TR/html4/loose.dtd">\n<html>\n<head>\n<meta http-equiv="Content-Type" content="text/html; charset=utf-8">\n<meta name="description" content= "Linuxエンジニア、ネットワークエンジニア、サーバ管理者(サーバー移行やサーバー仮想化構築)、SE/PG大募集!今、あなたの技術が求められています。リーディング・エッジ社は、ITエンジニアの生涯価値向上を支援する転職エージェント(人材紹介会社)です。">\n<meta name="keywords" content= "リーディング・エッジ,LE,SE,PG,ネットワークエンジニア,転職,派遣,キャリアアップ,未経験,スキルアップ,linux">\n<meta http-equiv="Imagetoolbar" content="no">\n<title>ITエンジニア支援(サーバ
In [96]: # パーサーは、継承して作る
from html.parser import HTMLParser
class TitleParser(HTMLParser):
def handle_data(self, data):
if self.get_starttag_text() == '<title>':
print(data)
In [97]: # パーサーのインスタンスを作って、HTML文書
を処理
p = TitleParser()
p.feed(html_str)
ITエンジニア支援(サーバー移行やサーバー仮
想化構築)|株式会社リーディング・エッジ社