Created
January 28, 2012 10:52
-
-
Save brokendish/1693936 to your computer and use it in GitHub Desktop.
pythonサンプル
This file contains hidden or 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 "------------------文字コードの確認" | |
print sys.stdin.encoding | |
print sys.stdout.encoding | |
print sys.stderr.encoding | |
#コマンドライン引数を取得 | |
print "-----------コマンドライン引数を取得" | |
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" | |
#range で10回ループさせる | |
print "-------------range(10)で10回ループさせる" | |
for i in range(10): | |
print i,':','AAAAA' | |
#ファイル存在確認 | |
print "-----------ファイル存在確認" | |
if os.path.isfile(inFile): | |
print inFile,'--入力ファイルを確認しました。' | |
else: | |
print inFile,"--入力ファイルがありません。" | |
#入力ファイルをオープン | |
print "------------入力ファイルをオープン" | |
i=open(inFile,"r") | |
#出力ファイルをオープン | |
print "-----------出力ファイルをオープン" | |
#a | |
#書き込み専用でファイルを開く。存在する場合は追加。ない場合は作成する。 | |
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 "------------------標準入力から取得" | |
getStr=raw_input('TypeWords : ') | |
print getStr | |
#複数行に渡る文字の記述 | |
print "------------------複数行に渡る文字の記述" | |
print """Python World | |
Brokendish | |
How TO""" | |
#raw | |
#文字列の扱い(特殊文字をエスケープする場合はR又はrをつける) | |
print "---------------raw文字列の扱い(特殊文字をエスケープする場合はR又はrをつける)" | |
print "c:¥M¥yData¥tmp¥data" | |
print r"c:¥M¥yData¥tmp¥data" | |
print R"c:¥M¥yData¥tmp¥data" | |
#文字の繰り返し | |
print "------------文字の繰り返し" | |
print "-" * 30 | |
#文字の長さを取得 | |
print "------------文字の長さを取得" | |
print len("abcdefghijk") | |
#数値を文字に変換 | |
print "------------数値を文字に変換" | |
print "abc" + str(10000) | |
#インデックスを指定して文字を取得 | |
print "------------インデックスを指定して文字を取得" | |
str = "brokendosh" | |
print str[0] | |
print str[1] | |
print str[2] | |
print str[3] | |
print str[4] | |
#文字の繰り返し | |
print "------------文字の繰り返し" | |
print "-" * 30 | |
#文字列のスライス(substringみたいなの) | |
print "------------文字列のスライス(substringみたいなの)" | |
str="abcde" | |
print str[0:3] | |
print str[2:3] | |
#文字列を数値に変換 | |
print "------------文字列を数値に変換" | |
str = "100" | |
print int(str) | |
#文字の繰り返し | |
print "------------文字の繰り返し" | |
print "-" * 30 | |
#while 文 | |
print "--------------wihile文" | |
num=0 | |
while num < 2: | |
print "hello" | |
num+=1 | |
#コマンド実行 | |
print "-------------コマンド実行" | |
print commands.getoutput("ls -la") | |
moji = commands.getoutput("ls") | |
print moji | |
#改行区切り | |
print "--------------改行区切り" | |
AAA=moji.split("\n") | |
print AAA[0] | |
#配列なくなるまでLOOP | |
print "-----------配列なくなるまでLOOP" | |
for k in AAA: | |
print k |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment