2010年3月9日火曜日
Python csvファイルをtextファイルに変換する
こんな感じかな
# import modules
import os.path
import csv
# functions
def csv2txt(inFilePath, outFilePath):
"""csv2txt convert csv file to text file.
Convert comma to tab."""
# file exists check
if not os.path.isfile(inFilePath):
print inFilePath + " is not exist."
return
# file extension check
root, ext = os.path.splitext(inFilePath)
if ext != ".csv":
print filepath + " is not .csv."
return
root, ext = os.path.splitext(outFilePath)
if ext != ".txt":
print filepath + " is not .txt."
return
# create output file
f = open(outFilePath,'w')
# read csv
reader = csv.reader(file(inFilePath, 'r'))
for row in reader:
newline = "\t".join(row)
f.write(newline + '\n')
f.close()
readerにclose()がなかった。自動的に開放されるみたい。
# import modules
import os.path
import csv
# functions
def csv2txt(inFilePath, outFilePath):
"""csv2txt convert csv file to text file.
Convert comma to tab."""
# file exists check
if not os.path.isfile(inFilePath):
print inFilePath + " is not exist."
return
# file extension check
root, ext = os.path.splitext(inFilePath)
if ext != ".csv":
print filepath + " is not .csv."
return
root, ext = os.path.splitext(outFilePath)
if ext != ".txt":
print filepath + " is not .txt."
return
# create output file
f = open(outFilePath,'w')
# read csv
reader = csv.reader(file(inFilePath, 'r'))
for row in reader:
newline = "\t".join(row)
f.write(newline + '\n')
f.close()
readerにclose()がなかった。自動的に開放されるみたい。
2010年3月8日月曜日
Python 亡記録2題
■ディレクトリ
作業ディレクトリーを変更する
import os
os.chdir(パス名)
現在の作業ディレクトリーを確認する
os.getcwd()
■for関連
2つのシーケンスを同時に回す
a = [1,2,3]
b = [4,5,6,7]
for x, y in zip(a,b):
print x, y
1 4
2 5
3 6
シーケンスでインデックスと同時に要素を取り出す
a = [5,6,7,8,9]
for i, x in enumerate(a):
print i, x
0 5
1 6
2 7
3 8
4 9
いつも忘れちゃうんだよな。歳だな。
作業ディレクトリーを変更する
import os
os.chdir(パス名)
現在の作業ディレクトリーを確認する
os.getcwd()
■for関連
2つのシーケンスを同時に回す
a = [1,2,3]
b = [4,5,6,7]
for x, y in zip(a,b):
print x, y
1 4
2 5
3 6
シーケンスでインデックスと同時に要素を取り出す
a = [5,6,7,8,9]
for i, x in enumerate(a):
print i, x
0 5
1 6
2 7
3 8
4 9
いつも忘れちゃうんだよな。歳だな。
2010年3月6日土曜日
Python リストのソート2
最初の要素は昇順、2番目の要素は降順
>>> score1 = [2,2,1,1,1,4,4,4]
>>> score2 = [3,4,4,2,1,1,2,2,]
>>> score = zip(score1,score2)
>>> sorted(score,key=lambda x:(x[0],-x[1]),reverse=False)
[(1, 4), (1, 2), (1, 1), (2, 4), (2, 3), (4, 2), (4, 2), (4, 1)]
ほほう
Python リストのソート
>>> name
['yamada', 'suzuki', 'yokoyama', 'takai', 'kashiwagi']
>>> old
[45, 21, 78, 16, 56]
>>> height
[166, 176, 170, 155, 149]
>>> weight
[78, 67, 82, 66, 53]
>>> country
['USA', 'Japan', 'Canada', 'Korea', 'UK']
>>> db = zip(name,old,height,weight,country)
>>> sorted(db,key=lambda x:(x[0],x[1]),reverse=False)
[('kashiwagi', 56, 149, 53, 'UK'), ('suzuki', 21, 176, 67, 'Japan'), ('takai', 1
6, 155, 66, 'Korea'), ('yamada', 45, 166, 78, 'USA'), ('yokoyama', 78, 170, 82,
'Canada')]
ほう
['yamada', 'suzuki', 'yokoyama', 'takai', 'kashiwagi']
>>> old
[45, 21, 78, 16, 56]
>>> height
[166, 176, 170, 155, 149]
>>> weight
[78, 67, 82, 66, 53]
>>> country
['USA', 'Japan', 'Canada', 'Korea', 'UK']
>>> db = zip(name,old,height,weight,country)
>>> sorted(db,key=lambda x:(x[0],x[1]),reverse=False)
[('kashiwagi', 56, 149, 53, 'UK'), ('suzuki', 21, 176, 67, 'Japan'), ('takai', 1
6, 155, 66, 'Korea'), ('yamada', 45, 166, 78, 'USA'), ('yokoyama', 78, 170, 82,
'Canada')]
ほう
Python リストの連結
忘れそうだから亡記録
>>> a
[0, 1, 2, 3, 4]
>>> b
['a', 'b', 'c', 'd', 'e']
>>> a+b
[0, 1, 2, 3, 4, 'a', 'b', 'c', 'd', 'e']
>>> zip(a,b)
[(0, 'a'), (1, 'b'), (2, 'c'), (3, 'd'), (4, 'e')]
>>> a
[0, 1, 2, 3, 4]
>>> b
['a', 'b', 'c', 'd', 'e']
>>> a+b
[0, 1, 2, 3, 4, 'a', 'b', 'c', 'd', 'e']
>>> zip(a,b)
[(0, 'a'), (1, 'b'), (2, 'c'), (3, 'd'), (4, 'e')]
Python リストの抽出
Pythonではリストの抽出にスライスに
リスト[開始位置:終了位置]
という構文を使うが、なんか慣れない。
>>> a = [0,1,2,3,4,5]
>>> a[0:3]
[0, 1, 2]
終了位置の値は返さないらしい。
なんか違和感を覚えてしまう。
リスト[開始位置:終了位置]
という構文を使うが、なんか慣れない。
>>> a = [0,1,2,3,4,5]
>>> a[0:3]
[0, 1, 2]
終了位置の値は返さないらしい。
なんか違和感を覚えてしまう。
登録:
投稿 (Atom)