#encoding:utf-8 f= open("C:/logs.txt","r") # 읽는 데이터는 ANSI 형태로 저장함 lines = f.readlines() f.close() aList = [] nameList = [] for i in lines : aList.append(i.split()) print aList[0][0] print len(lines) del aList[0] for j in aList: nameList.append(j[8]) print nameList.count(u"관형".encode('cp949')) newList = [] tList =[] for j in aList: newList.append([j[7]+j[8], j[9]]) for j in set(nameList): # set을 사용하면 중복되는 문자는 모두 제거함 tList.append((nameList.count(j),j)) #tList= [(a, nameList.count(a)) for a in set(nameList)] 이렇게 한줄로도 표기가능 tList.sort() for j in tList: print j[1], j[0]
또한 한글은 기존의 방법으로는 write()가 적용되지 않으니, 문자열이나 리스트 앞에는 'u', 뒤에는 '.encode('cp949')'를 붙일 수 있도록 합니다 ex) u"가나다 123".encode('cp949') , [u'가나다'.encode('cp949'), 12, 333..]
fr = open("C:\\aaa.txt", 'r') #파일 이름을 aaa.txt 로 만들었습니다. 허원 7969314 90 과 같은 정보가 여러줄 들어있음
fw = open("H:\\bbb.txt", 'w')
tempa = fr.readlines() #현상태(이름 학번 점수)를 텍스트파일에 출력
for b in tempa:
print b
c=b.split()
fw.writelines(c[0]+" "+c[2]+"\n") #이름 점수의 파일로 출력
fr.close() #파일 닫기
fw.close()