我试图遍历一堆文本文件,并将每个文件的第二行附加到列表中。以下是我的示例代码。看起来应该很接近了,但是什么都没有添加到我的列表中。
import os
directory = 'C:\\my_path\\'
i=0
list2 = []
for filename in os.listdir(directory):
with open(directory + filename) as infile:
try:
print(filename)
i=i+1
print(i)
data = infile.readlines()
for line in data:
if (line == 2):
list2.append(line)
infile.close()
except:
print(filename + ' is throwing an error')
print('DONE!!')
print(list2)
答案 0 :(得分:3)
写作时:
for line in data:
if (line == 2):
list2.append(line)
infile.close()
line
变量不是行的索引,而是行本身作为字符串。
还要注意,第二行的索引为1,而不是2,因为索引在Python中从0开始。
您至少应将此循环更改为:
for index, line in enumerate(data):
if (index == 1):
list2.append(line)
infile.close()
此外,正如@ bruno-desthuilliers所建议的那样,您不需要使用占用内存的readlines()
方法,而是可以像这样直接在文件上进行迭代:
#no infile.readlines() needed
for index, line in enumerate(infile):
if (index == 1):
list2.append(line)
infile.close()
最后,在将语句包装在infile.close()
块中时,不需要调用with
。通话已为您完成。
答案 1 :(得分:2)
当您测试Subscribe
时,是否在询问您从line == 2
读取的行是否等于infile
(从来没有)。相反,您需要一些计数器来测试您是否在第2行。甚至更好的是,只需对其进行索引:
2
答案 2 :(得分:1)
line == 2
试图将文本/字符串与数字2
进行比较,这无助于捕获正在读取的行的序数。
相反,只需跳过第一行并将下一行附加到结果列表中即可。
注意:
infile.readlines()
!close
时不需要with ...
文件处理程序import os
directory = 'C:\\my_path\\'
list2 = []
for filename in os.listdir(directory):
with open(directory + filename) as infile:
try:
print(filename)
next(infile)
list2.append(next(infile))
except:
print(filename + ' is throwing an error')
print('DONE!!!')
print(list2)
答案 3 :(得分:1)
尝试此版本:
import os
directory = 'C:\\my_path\\'
secondLines = []
for filename in os.listdir(directory):
try:
#Use open() because it is optimized and does not read the whole file into RAM
with open(directory + "\\" + filename) as infile:
for lineIndex, line in enumerate(infile):
if lineIndex == 1:
secondLines.append(line)
except:
print(filename + ' is throwing an error')
print(secondLines)
您的版本:
import os
directory = 'C:\\my_path\\'
i=0
list2 = []
for filename in os.listdir(directory):
#add "\\" to read the correct file
with open(directory + "\\" + filename) as infile:
try:
print(filename)
i=i+1
print(i)
data = infile.readlines()
#To get the second line, you have to use indexes
for line in range(len(data)):
#if line (index) equals 1, it is the second line (0th element is first)
if (line == 1):
#If the index of the line is 1, append it to the list
#data[line] = take the element on index 1 from list data. Indexing starts at 0
list2.append(data[line])
infile.close()
except:
print(filename + ' is throwing an error')
print('DONE!!')
print(list2)
答案 4 :(得分:0)
接下来是另一种优雅的实现方式,该方式可以避免迭代所有数据以及自动打开和关闭文件。
# With open should take care of automatic opening and closing of file. You don't need to close it explicitly.
with open(directory + filename) as infile:
try:
print(filename)
i=i+1
print(i)
skip_count = 0
line in infile:
skip_count += 1
if skip_count == 2:
list2.append(line)
break # This will go out of loop and you don't have to iterate through all the data
except:
print(filename + ' is throwing an error')