[英]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.