Created
December 30, 2012 16:48
-
-
Save HiroshiMatsumoto/4413754 to your computer and use it in GitHub Desktop.
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/en python | |
# -*- coding: utf-8 -*- | |
#1) 行数をカウントしたもの.確認にはwcコマンドを用いよ | |
#アプローチ:F.readlines()で行ごとに読み込みリスト化、 | |
#そのリストの要素数カウントをlen()関数で行う。 | |
f = open("../43KUMAMO.CSV", "r") | |
Content = f.readlines() | |
print len(Content) | |
f.close() |
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/en python | |
# -*- coding: utf-8 -*- | |
#2) タブ1文字につきスペース1文字に置換したもの.確認にはsedコマンド,trコマンド,もしくはexpandコマンドを用いよ. | |
#アプローチ:F.split(), F.join関数の利用 | |
#設定変更:タブ1文字につき→コンマ1文字につき | |
f = open("../43KUMAMO.CSV", "r") | |
for line in f.readlines(): | |
print " ".join(line.split(",")) | |
f.close() |
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/en python | |
# -*- coding: utf-8 -*- | |
#(3) 各行の1列目だけを抜き出したものをcol1.txtに,2列目だけを抜き出したものをcol2.txtとしてファイルに保存せよ.確認にはcutコマンドを用いよ | |
#アプローチ:for文とsplit関数の利用、書き出しにwrite関数 | |
f = open("../43KUMAMO.CSV", "r") | |
wfF = open("col1.txt","w"); | |
wfS = open("col2.txt","w"); | |
for line in f.readlines(): | |
line = line.split(",") | |
FirCol = line[0] | |
SecCol = line[1] | |
print u"1列目: %s" % FirCol, | |
print u"2列目: %s" % SecCol | |
wfF.write(FirCol+"\n") | |
wfS.write(SecCol+"\n") | |
f.close() | |
wfF.close() | |
wfS.close() |
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/en python | |
# -*- coding: utf-8 -*- | |
#(4) (3)で作ったcol1.txtとcol2.txtを結合し,元のタブ区切りテキストを復元したもの.確認にはpasteコマンドを用いよ. | |
#アプローチ:for文とsplit関数の利用、書き出しにwrite関数 | |
f = open("../43KUMAMO.CSV", "r") | |
wfF = open("first_col.col","w"); | |
wfS = open("second_col.col","w"); | |
wfR = open("復元.txt","w"); | |
for line in f.readlines(): | |
line = line.split(",") | |
FirCol = line[0] | |
SecCol = line[1] | |
wfR.write(FirCol+"\t"+SecCol+"\n") | |
f.close() | |
wfF.close() | |
wfS.close() | |
wfR.close() | |
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/en python | |
# -*- coding: utf-8 -*- | |
#(5) 自然数Nをコマンドライン引数にとり,入力のうち先頭のN行だけ.確認にはheadコマンドを用いよ. | |
#参考:http://osksn2.hep.sci.osaka-u.ac.jp/~taku/osx/python/readfile.html | |
#http://docs.python.jp/2.7/tutorial/stdlib.html#tut-command-line-arguments | |
#アプローチ: | |
import sys | |
N = sys.argv #intと仮定:エラーハンドリング未実装 | |
f = open("../43KUMAMO.CSV", "r") | |
lines = f.readlines() | |
for n in range(0, N) | |
print reqline[N] | |
f.close() | |
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/env python | |
# -*- coding: utf-8 -*- | |
# (6) 自然数Nをコマンドライン引数にとり,入力のうち末尾のN行だけ.確認にはtailコマンドを用いよ | |
# アプローチ: | |
import sys | |
N = int(sys.argv[1]) # $python 006.py 1 のようにファイル名の次に数値が来ると仮定 | |
#if(isinstance(N, int)):#http://www.gossamer-threads.com/lists/python/python/97153 | |
# print "it is an int" | |
# print N | |
#else: | |
# print "it is not an int" | |
# print N | |
f = open("43KUMAMO.CSV","r") | |
lines = f.readlines() | |
for num in range(0, N): #(5) | |
#for num in range(N, len(lines)):#(6) | |
print lines[num] | |
f.close() |
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/env python | |
# -*- coding: utf-8 -*- | |
#(7) 1コラム目の文字列の異なり数(種類数).確認にはcut, sort, uniq, wcコマンドを用いよ. | |
#line[a,b,c,d,e,f]とあったらaがline[0], line[1], line[2],・・・で異なるline要素が何個あるか | |
#アプローチ:aの内容をリスト | |
file = open("43KUMAMO.CSV", "r") | |
NewList = set() | |
for line in file.readlines(): | |
item = line.split(",") | |
NewList.add(item[0]) | |
print len(NewList) | |
file.close() |
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/env python | |
# -*- coding: utf-8 -*- | |
#(8) 各行を2コラム目の辞書順にソートしたもの(注意: 各行の内容は変更せずに並び替えよ).確認にはsortコマンドを用いよ(この問題は結果が合わなくてもよい). | |
import csv | |
file = open("43KUMAMO.CSV") | |
l = csv.reader(file)#http://docs.python.jp/2/library/csv.html | |
for row in file: | |
print csv | |
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/env python | |
# -*- encoding: utf-8 -*- | |
#(9) 各行を2コラム目,1コラム目の優先順位で辞書の逆順ソートしたもの(注意: 各行の内容は変更せずに並び替えよ).確認にはsortコマンドを用いよ(この問題は結果が合わなくてもよい). | |
#アプローチ:sorted関数の利用(key値の使用) | |
#参考: | |
#みんなのPython 柴田淳 | |
#http://docs.python.jp/2/howto/sorting.html | |
import csv | |
file = open("43KUMAMO.CSV","r") | |
contents = csv.reader(file) | |
NewList = [] | |
for line in contents: | |
#print line[3], | |
#print line[4], | |
#print line[5], | |
#print line[6], | |
#print line[7], | |
#print line[8] | |
NewList.insert(len(NewList), [line[4], line[5], line[7], line[8]]) | |
NewList.sort(key=lambda x:(x[1],x[0]), reverse=True) | |
for line in NewList: | |
print line[0], | |
print line[1], | |
print line[2], | |
print line[3] | |
file.close() | |
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/env python | |
#-*- encoding: utf-8 -*- | |
#(10) 各行の2コラム目の文字列の出現頻度を求め,出現頻度の高い順に並べよ.ただし,(3)で作成したプログラムの出力(col2.txt)を読み込むプログラムとして実装せよ.確認にはcut, uniq, sortコマンドを用い | |
#はしょり:(3)で作成したプログラムの出力(col2.txt)を読み込むプログラムとして実装せよ | |
#アプローチ: 読み込みファイルより[文字列, 頻度]のリストを作成 | |
#List = [["abd","asbd"],["wbd","zzdbd","wbd"],["abd","asbd"]] | |
#List = [[1,2],[3,4,5],[1,2]] | |
#print List | |
#NewList = set(List) | |
#print NewList | |
#上を実行すると以下の結果となる | |
""" | |
[['abd', 'asbd'], ['wbd', 'zzdbd', 'wbd'], ['abd', 'asbd']] | |
Traceback (most recent call last): | |
File "010.py", line 15, in <module> | |
NewList = set(List) | |
TypeError: unhashable type: 'list' | |
""" | |
#多重リストにsetは適用不可? | |
import csv | |
file = open("43KUMAMO.CSV","r") | |
Contents = csv.reader(file) | |
FreqList = [] #[対象文字列(str)、頻度(int)] | |
for lineContents in Contents: #コンテンツのループ | |
fInList = False #リスト用フラグ | |
for itemFreqList in FreqList: #リストのループ | |
if(lineContents[4]==itemFreqList[0]):#リストの中とのマッチング | |
itemFreqList[1] += 1 | |
fInList = True #リストの中にあったらフラグを立てる | |
if(fInList == False): #フラグがたってなかったら、 | |
#print lineContents[4] | |
FreqList.insert(len(FreqList), [lineContents[4], 0])#リストに追加 | |
FreqList.sort(key=lambda x:(x[1]))#, reverse=True) | |
for item in FreqList: | |
print item[0],item[1] | |
file.close() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment