简体   繁体   English

更快地读取和写入文本文件

[英]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. 我有以下代码,在其中读取输入列表,将其反斜线分割,然后将变量evid附加到evids数组。 Next, I open a file called evids.txt and write the evids to that file. 接下来,我打开一个名为evids.txt并写入evids到该文件。 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)
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]))

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM