Category Archives: python

「Python開発」reモジュールMatch Objectオブジェクトの利用方法

1.group([group1,…])
例:
>>> m=re.match(“(\w+) (\w+)”,”abcd efgh, chaj”)
>>> m.group()
‘abcd efgh’
>>> m.group(1)#
‘abcd’
>>> m.group(2)
‘efgh’
>>> m.group(1,2) #
(‘abcd’, ‘efgh’)
>>> m=re.match(“(?P<first_name>\w+) (?P<last_name>\w+)”,”sam lee”)
>>> m.group(“first_name”)
‘sam’
>>> m.group(“last_name”)
‘lee’

2.groups([default])
例:
>>> m=re.match(“(\d+)\.(\d+)”,”23.123″)
>>> m.groups()
(’23’, ‘123’)
>>> m=re.match(“(\d+)\.?(\d+)?”,”24″) #デフォルト”None”
>>> m.groups()
(’24’, None)
>>> m.groups(“0”)
(’24’, ‘0’)

3.groupdict([default])
例:
>>> m=re.match(“(\w+) (\w+)”,”hello world”)
>>> m.groupdict()
{}
>>> m=re.match(“(?P<first>\w+) (?P<secode>\w+)”,”hello world”)
>>> m.groupdict()
{‘secode’: ‘world’, ‘first’: ‘hello’}

4.split
例1:
>>> re.split(‘(\W+)’, ‘…words, words…’)
[”, ‘…’, ‘words’, ‘, ‘, ‘words’, ‘…’, ”]
>>> re.split(‘(\W+)’, ‘words, words…’)
[‘words’, ‘, ‘, ‘words’, ‘…’, ”]

例2:
>>> re.split(“\W+”,”words,words,works”,1)
[‘words’, ‘words,works’]
>>> re.split(“[a-z]”,”0A3b9z”,re.IGNORECASE)
[‘0A3’, ‘9’, ”]
>>> re.split(“[a-z]+”,”0A3b9z”,re.IGNORECASE)
[‘0A3’, ‘9’, ”]
>>> re.split(“[a-zA-Z]+”,”0A3b9z”)
[‘0’, ‘3’, ‘9’, ”]
>>> re.split(‘[a-f]+’, ‘0a3B9’, re.IGNORECASE)#re.IGNORECASEを利用
[‘0’, ‘3B9’]

5.re.findall(pattern, string[, flags])
例:
>>> re.findall(‘(\W+)’, ‘words, words…’)
[‘, ‘, ‘…’]
>>> re.findall(‘(\W+)d’, ‘words, words…d’)
[‘…’]
>>> re.findall(‘(\W+)d’, ‘…dwords, words…d’)
[‘…’, ‘…’]

6.subとsubn
例:
>>> re.sub(“\d”,”abc1def2hijk”,”RE”)
‘RE’
>>> x=re.sub(“\d”,”abc1def2hijk”,”RE”)
>>> x
‘RE’
>>> re.sub(“\d”,”RE”,”abc1def2hijk”,)
‘abcREdefREhijk’

>>> re.subn(“\d”,”RE”,”abc1def2hijk”,)
(‘abcREdefREhijk’, 2)

[python学習」randomモジュールの乱数を生成する方法まとめ

1.random()
乱数範囲: 0 <= n < 1.0
例:
>>> print random.random()
0.283212844881

2.uniform()
例:
>>> print random.uniform(1,10)
1.2558047703
>>> print random.uniform(10,1)
6.78930988147

3.randint()
#print random.randint(10, 20)

4.randrange()
形式 randrange([start],stop[, step])
例:random.randrange(10, 100, 2)

5.choice()
例:
print random.choice(“arkgame.com”)
print random.choice([“startnews24”, “is”, “a”, “it”, “site”])
print random.choice((“soft”, “code”, “in arkgame.com”))

6.shuffle()
形式 shuffle(x[,random]
例:
>>> p = [‘a’,’b’,’c’]
>>> random.shuffle(p)
>>> print p
[‘a’, ‘c’, ‘b’]

7.sample()
形式 random.sample(sequence,k)

>>> print random.sample([‘a_startnews24′,’b_startnews24′,’c_startnews24′,’d’],3) [‘a_startnews24’, ‘b_startnews24’, ‘c_startnews24’]

「python開発」osモジュールの使い方、ファイル、ディレクトリを作成など

1.pythonプロセス
os.listdir(dirname)
os.getcwd()
os.getpid()
os.chdir(dirname)
os.name
os.getenv()
os.system()  例 os.system(‘ls -a’)
os.path.split()

コード下記:
>>>> os.path.split(‘/a/b/c/startnews24.txt’)
(‘/a/b/c’, ‘startnews24.txt’)
os.path.isfile()とos.path.isdir()
os.path.exists()
>>> os.path.exists(‘a/b/a.txt’)
False

2.ファイルオブジェクトを作成
os.fdopen()
os.popen()

3.ファイルを操作
os.close(fd)
os.open(file,flags[,mode])
os.read(fd,n)
os.write(fd,str)

4.ファイルとディレクトリ
os.access(path,mode)
os.chdir(dirname)
os.getcwd()
os.chmod(path, mode)
os.chown(path, uid, gid)
os.listdir() 例 os.listdir(‘./’)
os.mkdir(path[,mode])
os.rmdir(path)
os.remove() ファイルを削除

5.プロセス管理
os.execl(path, arg0, arg1,…)
OK:os.execl( “/usr/bin/python “, “python “, 'test.py ', 'i ')
NG:os.execl( "/usr/bin/python ", "test.py ",
‘i ‘)
os.exec*()
6.システム情報
os.curdir
os.pardir
os.sep linux–‘/’,windows–‘\’
os.linesep Windows–‘\r\n’,Linux–‘\n’ Mac–‘\r’。

7.miscellaneous funciton
os.urandom(n)

「python開発」ftplibとos,socketモジュールを利用してファイルをダウンロードする

コード下記:
#!/usr/bin/python
# -*- coding: utf-8 -*-

import ftplib
import os
import socket

HOST = ‘ftp.mozilla.org’
DIRN = ‘pub/mozilla.org/webtools’
FILE = ‘bugzilla-3.6.9-to-3.6.10-nodocs.diff.gz’

def writedata(data):
f = open(FILE,’wb’)
try:
f.write(data)
finally:
f.close()

def main():
try:
f = ftplib.FTP(HOST)
except (socket.error, socket.gaierror):
print ‘ERROR:cannot reach ” %s”‘ % HOST
return
print ‘***ホストに接続 “%s”‘ % HOST

try:
f.login()
except ftplib.error_perm:
print ‘エラー: 匿名でログインできない’
f.quit()
return
print ‘*** Logged in as “anonymously”‘

try:
f.cwd(DIRN)
except ftplib.error_perm:
print ‘ERRORL cannot CD to “%s”‘ % DIRN
f.quit()
return
print ‘*** Changed to “%s” folder’ % DIRN

try:
#retrbinary()を呼び出す
f.retrbinary(‘RETR %s’ %FILE, writedata)
except ftplib.error_perm:
print ‘エラー: ファイルを読み取ることができません “%s”‘ %FILE
os.unlink(FILE)
else:
print ‘***ダウンロード”%s” to CWD’ % FILE

f.quit()
return

if __name__ == ‘__main__’:
main()

Python3でexcelファイルからテキストファイル(*txt)に変換するサンプルコード

サンプルコード:
import os
import glob
import xlrd3 as xlrd
import re

path = os.getcwd()
files = glob.glob(‘sourceExcel/*’)

for file in files:
wb = xlrd.open_workbook(file)

for sheetName in wb.sheet_names():
txtFile = open(‘outputTxts/’ + sheetName + ‘.txt’, mode=’w’, encoding=’utf-8′)
sheet = wb.sheet_by_name(sheetName)
for rownum in range(sheet.nrows):
v1 = sheet.cell(rownum, 0).value
if (type(v1) == float):
v1 = str(v1)
v1 = re.sub(‘\.0*$’, “”, v1)
v1 = v1.rstrip()
v2 = sheet.cell(rownum, 1).value
if (type(v2) == float):
v2 = str(v2)
v2 = re.sub(‘\.0*$’, “”, v2)
v2 = v2.rstrip()
dataStr = v1 + ‘=’ + v2 + ‘\n’
txtFile.write(dataStr)
txtFile.close()

Python3でテキストファイル(*txt)からexcelファイルに変換するサンプルコード

サンプルコード:
import os
import glob
path = os.getcwd()
files = glob.glob(‘../trunk/Resource/lang/ja_startnews24/*.txt’)
import xlwt3

if len(files) > 0:
wb = xlwt3.Workbook()
for file in files:
fileName = file.split(‘\\’)[1].split(‘.’)[0]
print(fileName)
ws = wb.add_sheet(fileName)
with open(file, encoding=’utf-8′) as a_file:
line_number = 0
for a_line in a_file:
a_line = a_line.rstrip()
mark = a_line.find(“=”)
ws.write(line_number, 0, a_line[0:mark])
ws.write(line_number, 1, a_line[mark+1:])
ws.col(0).width = 8000
ws.col(1).width = 40000
line_number += 1
a_file.close()
wb.save(‘langPack_ja_startnews24.xls’)

「Python入門」Pythonでメールを送信するサンプルコード

サンプルコード:
__author__ = ‘gg88’

import email,sys,os
import smtplib
from email.MIMEText import MIMEText
from email.MIMEMultipart import MIMEMultipart
from email.MIMEImage import MIMEImage

SENDER = ‘username@arkgame.com’
SMTPSERVER = ‘smtpcloud.sohu.com’
#RECEIVERS = ”
USERNAME = ‘username@arkgame.com’
APIKEY = ‘******’

def sendMail(subject, receivers, cc, content, atts):
msg = MIMEMultipart(‘related’)
msg[‘Subject’] = unicode(subject, “UTF-8″)
msg[‘From’] = SENDER
msg[‘To’] = receivers
msg[‘Cc’] = cc

#メール内容
if os.path.isfile(content):
if(content.split(‘.’)[-1]==’html’):
cont = MIMEText(open(content).read(),’html’,’utf-8′)
else:
cont = MIMEText(open(content).read(),’plain’,’utf-8′)
else:
cont = MIMEText(content, ‘plain’,’utf-8′)
msg.attach(cont)

#添付ファイルの処理
if atts != -1 and atts != ”:
for att in atts.split(‘,’):
os.path.isfile(att)
name = os.path.basename(att)
att = MIMEText(open(att).read(), ‘base64’, ‘utf-8’)
att[“Content-Type”] = ‘application/octet-stream’
#utf-8
att[“Content-Disposition”] = ‘attachment; filename=%s’ % name.decode(‘utf-8’).encode(‘gbk’)
msg.attach(att)

smtp = smtplib.SMTP()
smtp.connect(SMTPSERVER)
smtp.login(USERNAME, APIKEY)
for recev in receivers.split(‘,’):
smtp.sendmail(SENDER,recev, msg.as_string())
for c in cc.split(‘,’):
smtp.sendmail(SENDER,c, msg.as_string())
smtp.quit()

def main():
print “start send mail[sendmail.py]”
subject = sys.argv[1]
receivers = sys.argv[2]
#cc = sys.argv[3]
leng = len(sys.argv)
if leng == 3:
cc = “”
content = “”
atts = -1
elif leng == 4:
print “The parameters is not currect!”
sys.exit(0)
elif leng == 5:
cc = sys.argv[3]
content = sys.argv[4]
atts = -1
elif leng == 6:
cc = sys.argv[3]
content = sys.argv[4]
atts = sys.argv[5]
sendMail(subject, receivers, cc, content, atts)
print “finish send mail[sendmail.py]”

if __name__==’__main__’:
main()

Pythonで時間処理のフォーマット

Directive Meaning Example Notes
%a Weekday as locale’s abbreviated name. Sun, Mon, …, Sat (en_US);

So, Mo, …, Sa (de_DE)

(1)
%A Weekday as locale’s full name. Sunday, Monday, …, Saturday (en_US);

Sonntag, Montag, …, Samstag (de_DE)

(1)
%w Weekday as a decimal number, where 0 is Sunday and 6 is Saturday. 0, 1, …, 6
%d Day of the month as a zero-padded decimal number. 01, 02, …, 31
%b Month as locale’s abbreviated name. Jan, Feb, …, Dec (en_US);

Jan, Feb, …, Dez (de_DE)

(1)
%B Month as locale’s full name. January, February, …, December (en_US);

Januar, Februar, …, Dezember (de_DE)

(1)
%m Month as a zero-padded decimal number. 01, 02, …, 12
%y Year without century as a zero-padded decimal number. 00, 01, …, 99
%Y Year with century as a decimal number. 1970, 1988, 2001, 2013
%H Hour (24-hour clock) as a zero-padded decimal number. 00, 01, …, 23
%I Hour (12-hour clock) as a zero-padded decimal number. 01, 02, …, 12
%p Locale’s equivalent of either AM or PM. AM, PM (en_US);

am, pm (de_DE)

(1), (2)
%M Minute as a zero-padded decimal number. 00, 01, …, 59
%S Second as a zero-padded decimal number. 00, 01, …, 59 (3)
%f Microsecond as a decimal number, zero-padded on the left. 000000, 000001, …, 999999 (4)
%z UTC offset in the form +HHMM or -HHMM (empty string if the the object is naive). (empty), +0000, -0400, +1030 (5)
%Z Time zone name (empty string if the object is naive). (empty), UTC, EST, CST
%j Day of the year as a zero-padded decimal number. 001, 002, …, 366
%U Week number of the year (Sunday as the first day of the week) as a zero padded decimal number. All days in a new year preceding the first Sunday are considered to be in week 0. 00, 01, …, 53 (6)
%W Week number of the year (Monday as the first day of the week) as a decimal number. All days in a new year preceding the first Monday are considered to be in week 0. 00, 01, …, 53 (6)
%c Locale’s appropriate date and time representation. Tue Aug 16 21:30:00 1988 (en_US);

Di 16 Aug 21:30:00 1988 (de_DE)

(1)
%x Locale’s appropriate date representation. 08/16/88 (None);

08/16/1988 (en_US);

16.08.1988 (de_DE)

(1)
%X Locale’s appropriate time representation. 21:30:00 (en_US);

21:30:00 (de_DE)

(1)
%% A literal ‘%’ character. %

 使い方

>>> t = time
>>> t.strftime(“%m”)
’11’
>>> t.strftime(“%d”)
’08’
>>> t.strftime(“%y”)
’14’
>>> t.strftime(“%y/%m/%d”)
’14/11/08′

PythonでURLの‘%2B 20%’‘3B’をエンコーディングする

Pythonコード:
import os,sys;

path = sys.path[0]
os.chdir(path)
encode_list = ‘encode_list.txt’
result = path + ‘\\results’

def get_encode():  //エンコーディング
encode_file = open(path + ‘\\’+ encode_list)
encode = dict()
for line in encode_file:
if line!=’\n’ and len(line) >1:
if line.find(‘read me’) <0:
encode[line[1:].strip()] = line[0]
return encode

def get_files(): //ファイルを取得
files = os.listdir(path)
file_list = list()
for file in files:
if file.endswith(‘.txt’) and file!= encode_list:
file_list.append(file)
return file_list

def relace_url_encode(strPri,dicEncode): //URLをエンコーディング
items = dicEncode.items()
for (key,value) in items:
if strPri.find(key):
strPri = strPri.replace(key,value)
return strPri

def create_result():  //結果を作成
if not os.path.isdir(result):
os.makedirs(result)

def write_result(filePri,strText):  //結果を書き込む
fp = open(result+’\\’+filePri,’w+’)
fp.write(strText)
fp.close()

create_result()
encode = get_encode()
file_list = get_files()
for ff in file_list:
try:
f = open(ff)
text = f.read()
finally:
f.close()
temp = relace_url_encode(text,encode)
temp = temp.replace(‘&’,’\n’)
write_result(ff,temp)

PythonでBeautiful soupモジュールをインストールする方法

1.バージョン
BeautifulSoup4-4.1.3

2.パッケージをインポート
import bs4
from bs4 import BeautifulSoup

3. コマンド
python setup.py build
python setup.py install