geeb.24
geeb.24

Reputation: 557

Making the reading and writing of text files quicker

I have the following code, where I read an input list, split it on its backslash, and then append the variable evid to the evids array. Next, I open a file called evids.txt and write the evids to that file. How do I speed up/reduce the number of lines in this code? Thanks.

evids = []
with open('evid_list.txt', 'r') as infile:
    data = infile.readlines()
    for i in data:
        evid = i.split('/')[2]
        evids.append(evid)

with open('evids.txt', 'w') as f:
    for i in evids:
        f.write("%s" % i)

Upvotes: 0

Views: 43

Answers (1)

Osman Mamun
Osman Mamun

Reputation: 2882

with open('evid_list.txt', 'r') as infile, open('evids.txt', 'w') as ofile:
    for line in infile:
        ofile.write('{}\n'.format(line.split('/')[2]))

Upvotes: 3

Related Questions