LoginSignup
6
5

More than 5 years have passed since last update.

pythonで巨大なファイルをソートする

Last updated at Posted at 2016-02-29

subprocess.Popen

python使
sorted()使

Unixsort使

import subprocess
def sorted_file_generator(filename):
    proc = subprocess.Popen(['sort', filename], stdout=subprocess.PIPE)
    while True:    # while を使って1行ずつ受け取る
        line = proc.stdout.readline()
        if line:
            yield line.decode('utf-8').strip()    # 返り値はバイトコードなのでデコードする
        else:
            break

この例はpython3です。
2の場合は、バイトの取り扱いが違うはず

6
5
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
6
5