게시판 즐겨찾기
편집
드래그 앤 드롭으로
즐겨찾기 아이콘 위치 수정이 가능합니다.
python 코드 메모리가 초과해서 전부 못돌리는거 같은데 도움요청이요ㅠ
게시물ID : programmer_17532짧은주소 복사하기
작성자 : 뭉뭉이아빠
추천 : 0
조회수 : 9263회
댓글수 : 10개
등록시간 : 2016/06/08 11:12:28
옵션
  • 베스트금지
  • 본인삭제금지
  • 외부펌금지
일단 코드는요


# -*- coding: cp949 -*-

import sys, os, time
import openpyxl
from os import listdir
from os.path import isfile, join
import os, glob
import pdb
import gc
#pdb.set_trace()

def filerun(textpath,rownum):
    f = open(textpath, 'r+',)
    row_list = []
    colnum = 1
    for row in f :
        row_list.append(row.split('#'))
        
    for col in row_list:
        for n in range(0, pNum):
            if col[0] == pList[n]:
                colnum = colnum + 1
                
                for i in range(len(title)):
                    l = [2, 3, 4, 5, 6, 7, 8, 10, 11, 13, 14, 15]
                    for ip in l:
                        if i == ip:
                            savews = savewb[i].active
                            savews.cell(row=(rownum), column= (colnum) ).value = col[i]
                            savews.cell(row=(rownum), column= 1 ).value = col[1]
    f.close()



#print log
now = time.localtime()
print '[' + str(now.tm_year) + '/' + str(now.tm_mon) + '/' + str(now.tm_mday) + ' ' + str(now.tm_hour) + ':' + str(now.tm_min) + ':' + str(now.tm_sec) + '] Start Process'

pNum = int(raw_input('Enter the number of station : ')) 
print 'Enter the station number : '

tmpString = raw_input()
pList = tmpString.split(' ')    

title = ['code', 'ymdhm', 'latitude', 'longitude', 'height ', 'WD1(x10)', 'WS1(x10)', 'Temp(x10)', 'humidity(x10)', 'noname1', 'seaLpress(x10)', 'precOX(x10)', 'noname2', 'DPrec(x10)', 'Prec15(x10)', 'Prec60(x10)', 'noname3', 'noname4', 'noname5']

savewb = list()
count = list()  
for i in range(0, len(title)):   
    count.append(list())
    count[i] = 1
    savewb.append(list())
    savewb[i] = openpyxl.Workbook() 

#Get root directory path
sourcepath = raw_input('Root path : ')    

#Set save directory path
savepath = raw_input('save path : ')

if not os.path.exists(savepath):  
    os.makedirs(savepath)

#Get directory's file list
for i in range(0, len(title)):
    rcount = 1
    savews = savewb[i].active
    
    for jijum in pList :
        savews.cell(row=1, column= rcount + 1).value = jijum
        rcount = rcount + 1


years = listdir(sourcepath)




for y in years :
    ypath = os.path.join(sourcepath, y)
    months = listdir(ypath)
    rownum1 = 2
    
    for m in months :
        ympath = os.path.join(ypath, m)
        days = listdir(ympath)
        
        for d in days :
            ymdpath = os.path.join(ympath, d)
            textfiles = [ join(ymdpath,f) for f in listdir(ymdpath) if isfile(join(ymdpath,f)) and '.txt' in  f]
            
            for textfile in textfiles:
                filerun(textfile, rownum1)
                rownum1 = rownum1 + 1

            gc.collect()
                 
for i in range(len(title)):
    l = [2, 3, 4, 5, 6, 7, 8, 10, 11, 13, 14, 15]
    for ip in l:
        if i == ip:
            savewb[i].save(os.path.join(savepath, (str(title[i]) + '.xlsx')))

    






위와 같이 만들어진 코드이고 어떤 코드인지 설명 드리면

txt 파일 안에 있는 데이터가

12#201105010000#36.5333#126.3167#45.70#0#0#125#956#10002#10056#0#0#600#0#0#0#0#=

이런 형식으로 약 몇백 줄 정도 있는 파일이 있습니다.

그래서 # 으로 split 한 다음에

가장 앞에 위치한 숫자가 타겟인데요

제가 설정한 숫자가 맞을 경우 엑셀에 기록을 하여 저장을 하는 형태입니다.

그런데 돌려야할 txt 파일이 한 20기가 정도가 되는데요

MemoryError 가 뜨고

작업관리자에서 메모리 리소스를 보면 python은 2기가를 꽉 채워서 돌다가 결국 에러가 나더라구요 ㅠㅠ

적은 파일은 돌리면 이상없이 돌아가고 결과도 잘 생성되는데

한번에 다 할려고 하면 저렇게 오류가 발생 해버리는데 어떻게 처리를 해줘야 될까요 ㅠㅠ...

파일도 계속해서 close 하고 가비지콜렉터도 비워주는데 

어떻게 해야 할지...

프로그래밍 고수님의 도움이 필요합니다 ㅠㅠ












전체 추천리스트 보기
새로운 댓글이 없습니다.
새로운 댓글 확인하기
글쓰기
◀뒤로가기
PC버전
맨위로▲
공지 운영 자료창고 청소년보호