Created
August 9, 2014 06:59
-
-
Save brokendish/e2ca7f8745333d548d2e to your computer and use it in GitHub Desktop.
Pythonサンプル:実行方法 ./pythonSMP.py 123 456
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/python | |
# -*- coding: UTF-8 -*- | |
#CSV | |
#モジュールをインポート | |
#デミリッタを使用してawkのような処理を行う事が可能 | |
import csv | |
#標準出力に出力したいのでsysをインポート | |
import sys | |
#ファイルの存在確認をしたいのでosをインポート | |
import os | |
#コマンドを実行 | |
import commands | |
#文字コードの確認 | |
print "*" * 40 | |
print "文字コードの確認" | |
print "*" * 40 | |
print sys.stdin.encoding | |
print sys.stdout.encoding | |
print sys.stderr.encoding | |
#コマンドライン引数を取得 | |
print "*" * 40 | |
print "コマンドライン引数を取得" | |
print "*" * 40 | |
hiki1=sys.argv[1] | |
hiki2=sys.argv[2] | |
print "argv1: " + sys.argv[1] | |
print "argv2: " + sys.argv[2] | |
inFile="./aaa.lst" | |
outFile="./out_aaa.lst" | |
#-------------------------------------------- | |
#下記で使うファイル作成 | |
print "*" * 40 | |
print "ファイル[",inFile,"]の準備" | |
print "*" * 40 | |
str = """Python World@123 | |
Brokendish@456 | |
How TO@789""" | |
print "ファイル[",inFile,"]作成" | |
f = open(inFile, 'w') # 書き込みモードで開く | |
f.write(str) # 文字列をファイルに書き込む | |
f.close() # ファイルを閉じる | |
#ファイル表示 | |
print "作成したファイル表示" | |
for line in open(inFile, 'r'): | |
print line | |
#-------------------------------------------- | |
#range で10回ループさせる--------------------------------------------① | |
print "*" * 40 | |
print "range(10)で10回ループさせる①" | |
print "*" * 40 | |
for i in range(10): | |
print i,':','AAAAA' | |
#ファイル存在確認----------------------------------------------------② | |
print "*" * 40 | |
print "ファイル存在確認②" | |
print "*" * 40 | |
if os.path.isfile(inFile): | |
print inFile,'--入力ファイルを確認しました。' | |
#入力ファイルをオープン | |
print "------------入力ファイルをオープン" | |
i=open(inFile,"r") | |
else: | |
print inFile,"--入力ファイルがありません。" | |
#出力ファイルをオープン------------------------------------------------③ | |
print "*" * 40 | |
print "出力ファイルをオープン③" | |
print "*" * 40 | |
#書き込み専用でファイルを開く。存在する場合は追加。ない場合は作成する。 | |
print "-----------a 書き込み専用でファイルを開く。存在する場合は追加。ない場合は作成する。" | |
o=open(outFile,"w") | |
for j in csv.reader(i,delimiter="@"): | |
print j[0] + "---" + j[1] | |
# ファイルに書き込み | |
o.write(j[0] + "---" + j[1] + "/n") | |
# 標準出力に表示 | |
sys.stdout.write(j[0] + "---" + j[1] + "/n") | |
#標準入力から取得----------------------------------------------------④ | |
print "*" * 40 | |
print "標準入力から取得④" | |
print "*" * 40 | |
getStr=raw_input('TypeWords : ') | |
print getStr | |
#複数行に渡る文字の記述-----------------------------------------------⑤ | |
print "*" * 40 | |
print "複数行に渡る文字の記述⑤" | |
print "*" * 40 | |
print """Python World | |
Brokendish | |
How TO""" | |
#文字列の扱い(特殊文字をエスケープする場合はR又はrをつける)---------------⑥ | |
print "*" * 40 | |
print "raw文字列の扱い(特殊文字をエスケープする場合はR又はrをつける)⑥" | |
print "*" * 40 | |
print "c:¥M¥yData¥tmp¥data" | |
print r"c:¥M¥yData¥tmp¥data" | |
print R"c:¥M¥yData¥tmp¥data" | |
#文字の長さを取得----------------------------------------------------⑦ | |
print "*" * 40 | |
print "文字の長さを取得⑦" | |
print "*" * 40 | |
print len("abcdefghijk") | |
#インデックスを指定して文字を取得----------------------------------------⑧ | |
print "*" * 40 | |
print "インデックスを指定して文字を取得⑧" | |
print "*" * 40 | |
str = "brokendosh" | |
print str[0] | |
print str[1] | |
print str[2] | |
print str[3] | |
print str[4] | |
#文字列のスライス(substringみたいなの)----------------------------------⑨ | |
print "*" * 40 | |
print "文字列のスライス(substringみたいなの)⑨" | |
print "*" * 40 | |
str="abcde" | |
print str[0:3] | |
print str[2:3] | |
#文字列を数値に変換---------------------------------------------------⑩ | |
print "*" * 40 | |
print "文字列を数値に変換⑩" | |
print "*" * 40 | |
str = "100" | |
print int(str) | |
#while 文------------------------------------------------------------⑪ | |
print "*" * 40 | |
print "wihile文⑪" | |
print "*" * 40 | |
num=0 | |
while num < 2: | |
print "hello" | |
num+=1 | |
#コマンド実行----------------------------------------------------------⑫ | |
print "*" * 40 | |
print "コマンド実行⑫" | |
print "*" * 40 | |
print commands.getoutput("ls -la") | |
moji = commands.getoutput("ls") | |
print moji | |
#改行区切り-----------------------------------------------------------⑬ | |
print "*" * 40 | |
print "改行区切り⑬" | |
print "*" * 40 | |
AAA=moji.split("\n") | |
print AAA[0] | |
#配列なくなるまでLOOP---------------------------------------------------⑭ | |
print "*" * 40 | |
print "配列なくなるまでLOOP⑭" | |
print "*" * 40 | |
for k in AAA: | |
print k | |
#Pythonではswitch文は無いのでif文で代用する------------------------------⑮ | |
print "*" * 40 | |
print "Pythonではswitch文は無いのでif文で代用する⑮" | |
print "*" * 40 | |
print "-----------IF文(Or)" | |
str_0 = "ABC" | |
str_a = "abc" | |
str_b = "ABC" | |
str_c = "123" | |
if str_0 == str_a or str_0 == str_b: | |
print "str_0はabcかABCだね:",str_0 | |
elif str_0 == str_c: | |
print "str_0は123だね:",str_0 | |
print "-----------IF文(In)" | |
if str_0 in(str_a,str_0): | |
print "str_0はabcかABCだね:",str_0 | |
elif str_0 == str_c: | |
print "str_0は123だね:",str_0 | |
print "------------文字の繰り返し" | |
print "*" * 30 | |
#ファイル削除-------------------------------------------------------------⑯ | |
print "*" * 40 | |
print "ファイル[",inFile,"]削除⑯" | |
print "ファイル[",outFile,"]削除⑯" | |
print "*" * 40 | |
os.remove(inFile) | |
os.remove(outFile) | |
#ファイル一覧表示---------------------------------------------------------⑰ | |
print "*" * 40 | |
print "ファイル一覧表示⑰" | |
print "*" * 40 | |
files = os.listdir('./') | |
for file in files: | |
print file | |
#辞書型(ハッシュマップみたいなもの)------------------------------------------⑱ | |
print "*" * 40 | |
print "辞書型(ハッシュみたいなもの)⑱" | |
print "*" * 40 | |
dic = {} | |
dic['T']='Tokyo' | |
dic['K']='Kanagawa' | |
dic['C']='Ciba' | |
print dic['T'] | |
print dic['K'] | |
print dic['C'] | |
print "---値を取得--------------------------" | |
print dic.get('T') | |
print dic.get('K') | |
print dic.get('C') | |
print "---Key情報を取得---------------------" | |
for key in dic.keys(): | |
print key | |
print "---値を全部取得----------------------" | |
for ret in dic.values(): | |
print ret | |
print "---Keyと値を全部を取得----------------" | |
for ret in dic.items(): | |
print ret | |
print "---値を入れ替える------Tokyo>>Toyota--" | |
dic['T']='Toyota' | |
print dic.get('T') | |
print "---値を削除--dic['T']----------------" | |
del dic['T'] | |
print "---Keyと値を全部を取得----------------" | |
for ret in dic.items(): | |
print ret |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment