我认为列表理解会比调用.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)