.append
リスト内包表記は、各行を呼び出すよりも高速だと思います
from itertools import chain
with open('file.txt') as f:
lines = chain.from_iterable([l.replace(r'"','').rstrip('\n').split('\t',1) for l in f])
編集:フラット化されたリストを生成するため
>>>
['CARMILLA', '35', 'JONATHAN R', 'AA2', 'M', '3', 'EMMA', '350', 'OLD', 'AA']
非平坦化バージョン:
with open('file.txt') as f:
lines = [l.replace(r'"','').rstrip('\n').split('\t',1) for l in f]
そして、あるタイミングで、OPが最速であることが判明しましたか?
import timeit
print("chain, list",timeit.timeit(r"""
with open('file.txt') as f:
lines = chain.from_iterable([l.replace(r'"','').rstrip('\n').split('\t',1) for l in f])""",setup="from itertools import chain",number=1000))
print("flat ",timeit.timeit(r"""
with open('file.txt') as f:
lines = [l.replace(r'"','').rstrip('\n').split('\t',1) for l in f]""",setup="from itertools import chain",number=1000))
print("op's ",timeit.timeit(r"""temp = []
fp = open('file.txt', "r")
for line in fp:
temp.append(line.replace("\"","").rstrip("\n").split("\t"))
""",number=1000))
print("jamlyks ",timeit.timeit(r"""
with open('file.txt', 'rb') as f:
r = csv.reader(f, delimiter=' ', skipinitialspace=True)
list(chain.from_iterable(r))""",setup="from itertools import chain; import csv",number=1000))
print("lennart ",timeit.timeit(r"""
list(csv.reader(open('file.txt'), delimiter='\t', quotechar='"'))""",setup="from itertools import chain; import csv",number=1000))
収量
C:\Users\Henry\Desktop>k.py
('chain, list', 0.04725674146159321)
('my flat ', 0.04629905135295972)
("op's ", 0.04391255644624917)
('jamlyks ', 0.048360870934994915)
('lennart ', 0.04569112379085424)