split() returns you a list. And then you are trying to access the first,second and third element by
line = [int(i) for i in line]
a = line[0]
b = line[1]
c = line[2]
Instead of that you can directly say a,b,c = line.split() then a will contain line[0], b will contain line[1] and c will contain line[2]. This should save you some time.
with open(sample) as f:
for line in f:
a,b,c = line.split()
do_someprocess()
An example:
with open("sample.txt","r") as f:
for line in f:
a,b,c = line.split()
print a,b,c
.txt file
12 34 45
78 67 45
Output:
12 34 45
78 67 45
EDIT :
I thought of elaborating on it.I have used timeit() module to compare the time taken by the code to run. Please let me know if I'm doing something wrong here.The following is the OP way of writing the code.
v = """ with open("sample.txt","r") as f:
for line in f:
line = line.split()
line = [int(i) for i in line]
a = line[0]
b = line[1]
c = line[2]"""
import timeit
print timeit.timeit(stmt=v, number=100000)
Output:
8.94879606286 ## seconds to complete 100000 times.
The following is my way of writing the code.
s = """ with open("sample.txt","r") as f:
for line in f:
a,b,c = [int(s) for s in line.split()]"""
import timeit
print timeit.timeit(stmt=s, number=100000)
Outputs :
7.60287380216 ## seconds to complete same number of times.
split()andint()are the functions taking the most time?