Skip to content

CHICPRO

  • Life Log
  • Cycling Log
  • Photo Log
    • Portrait
    • Landscape
    • Flower
    • Etc
  • Coding Log
  • Information

[Python] 파일에서 필요한 데이터를 추출해 파일로 저장하기 #3

2017-03-13 by 편리

오늘은 파이썬의 mmap 모듈과 multiprocessing 모듈을 이용해서 텍스트 파일의 데이터를 추출해 재가공하는 부분을 작성해봤다. 대용량 파일의 처리는 mmap 모듈을 사용하는 것이 효율적이라고 해서 적용을 했는데.. 일단 10만건 기준으로 기존보다는 10초정도 빨라진다. 추출 데이터를 좀 더 늘려서 테스트를 진행해봐야 할 것 같다.

# _*_ coding: utf-8 _*_

from multiprocessing import Process
import mmap
import time
import datetime
import locale

seller = []
exclude = []

def get_exce_time(start, end):
    return datetime.timedelta(seconds=(end - start))

def number_format(num, places=0):
    """Format a number according to locality and given places"""
    locale.setlocale(locale.LC_ALL, "")
    return locale.format("%.*f", (places, num), True)

def file_read():
    start = time.time()

    results = {}

    with open('seller_id.txt', 'r+b') as f:
        mm = mmap.mmap(f.fileno(), 0)

        for line in iter(f.readline, b''):
            str = line.split(b"\t")
            id = str[4].strip()

            if id not in seller:
                seller.append(id)

        mm.close()

        results['seller'] = seller

    with open('exclude_item_id.txt', 'r+b') as f:
        mm = mmap.mmap(f.fileno(), 0)

        for line in iter(f.readline, b''):
            str = line.strip()

            if str not in exclude:
                exclude.append(str)

        mm.close()

        results['exclude'] = exclude

    end = time.time()
    print("File Reading Time : {0}".format(get_exce_time(start, end)))

    return results

def find_item_code(data, process, idx, limit):
    seller = data['seller']
    exclude = data['exclude']

    for k, s_id in enumerate(seller):
        start = time.time()
        item = []

        if k % process != idx:
            continue

        with open('AllDataBasedOnDB.dat', 'r+b') as f:
            mm = mmap.mmap(f.fileno(), 0)

            for line in iter(mm.readline, b''):
                if limit > 0 and len(item) == limit:
                    break

                if line.find(s_id) == -1:
                    continue

                str = line.split(b"\t")
                code = str[0].strip()

                if code not in exclude and code not in item:
                    item.append(code)

            # txt 파일생성
            with open('./out/' + s_id.decode() + '.txt', 'w+b') as out:
                out.write(b'\n'.join(item))

            end = time.time()
            print("{0} Process #{1} Running Time : {2} Count: {3}".format(s_id.decode(), idx, get_exce_time(start, end),
                                                                          number_format(len(item))))

            mm.close()

if __name__ == '__main__':
    data = file_read()

    # 실행 프로세스 수
    process = 4

    # 추출한 레코드 수 0으로 설정하면 모든 레코드 추출
    limit = 0

    procs = [Process(target=find_item_code, args=((data, process, i, limit,))) for i in range(process)]
    for p in procs: p.start()

mmap 모듈도 그렇고 multiprocessing 모듈도 그렇고.. 파이썬을 접한지가 오래 되지 않아서 코드를 맞게 작성했는지 모르겠다.

Post navigation

Previous Post:

2017년 3월 12일 양수역

Next Post:

[PHP] 영카트5 원본 이미지 비율유지하며 썸네일 생성

Leave a Reply Cancel reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.

Recent Posts

  • php 배열 연산에서 + 와 array_merge 의 차이
  • pcntl_fork 를 이용한 다중 프로세스 실행
  • 아이폰 단축어를 이용하여 주중 공휴일엔 알람 울리지 않게 하기
  • 구글 캘린더 전체일정 재동기화
  • OpenLiteSpeed 웹서버에 HTTP 인증 적용
  • OpenLiteSpeed 웹어드민 도메인 연결
  • WireGuard를 이용한 VPN 환경 구축
  • Ubuntu 22.04 서버에 OpenLiteSpeed 웹서버 세팅
  • 맥 vim 세팅
  • 우분투 시스템 터미널쉘 zsh 로 변경

Recent Comments

  • 편리 on 업무관리용 그누보드 게시판 스킨
  • 임종섭 on 업무관리용 그누보드 게시판 스킨
  • 캐논 5D 펌웨어 | Dslr 펌웨어 업그레이드 방법 82 개의 베스트 답변 on 캐논 EOS 30D 펌웨어 Ver 1.0.6 , EOS 5D 펌웨어 Ver 1.1.1
  • Top 5 캐논 5D 펌웨어 Top 89 Best Answers on 캐논 EOS 30D 펌웨어 Ver 1.0.6 , EOS 5D 펌웨어 Ver 1.1.1
  • 편리 on 워드프레스 애니메이션 gif 파일을 mp4로 변환하여 출력하기
  • 임팀장 on 워드프레스 애니메이션 gif 파일을 mp4로 변환하여 출력하기
  • 편리 on Notepad++ NppFTP 플러그인 수동 설치
  • paul-j on Notepad++ NppFTP 플러그인 수동 설치
  • YS on Windows 10 iCloud 사진 저장 폴더 변경
  • 편리 on Docker를 이용한 Centos7 + httpd + php 5.4 개발환경 구축

Meta

  • Log in
  • Entries feed
  • Comments feed
  • WordPress.org
© 2025 CHICPRO | Built using WordPress and SuperbThemes