[英]Appending Specific Lines from a File, inside For Loop to open each file
I have 4 folders full of txt documents.我有 4 个文件夹,里面装满了 txt 文档。 I used the code below to extract all the txt and append them to a list.
我使用下面的代码将所有 txt 和 append 提取到一个列表中。
Doc1 = glob.glob('path*.txt')
Doc2 = glob.glob('path*.txt')
Doc3 = glob.glob('path*.txt')
Doc4 = glob.glob('path*.txt')
lines = []
for file in Doc1: ### repeated this block for Doc2, Doc3 and Doc4 ####
f = open(file,'r')
lines.append(f.readlines())
f.close()
This code above worked just fine.上面的代码工作得很好。 However, now what I want to do is:
但是,现在我想做的是:
for file in Doc1:
f = open(file,'r') #this opens the file
for line in file: #for each line in the file
tag = False #tag set to False, initially
if line.startswith('text'): #if it starts with this text then:
tag = True #tag changes to True
elif 'end text' in line: #if it this text is in the line then:
tag = False #tag stays false
lines.append(f.readlines()) #append this line (but now tag stays False)
elif tag: # if tag is true, then append that line
lines.append(f.readlines())
f.close()
This code runs, as in, I do not get any warnings or errors.此代码运行,如我没有收到任何警告或错误。 But no lines append to Lines.
但是没有线 append 到线。 TIA for any advice and assistance.
TIA 寻求任何建议和帮助。
this is because you are using the line
, is the file,in string form, not readed, to do this you can do:这是因为您正在使用
line
,是文件,以字符串形式,未读取,为此您可以执行以下操作:
for line in f:
# write code here
so your code becomes like this:所以你的代码变成这样:
import glob
Doc1 = glob.glob('path*.txt')
Doc2 = glob.glob('path*.txt')
Doc3 = glob.glob('path*.txt')
Doc4 = glob.glob('path*.txt')
lines = []
for file in Doc1:
f = open(file,'r') #this opens the file
for line in f: #for each line in the file
tag = False #tag set to False, initially
if line.startswith('text'): #if it starts with this text then:
tag = True #tag changes to True
elif 'end text' in line: #if it this text is in the line then:
tag = False #tag stays false
lines.append(f.readlines()) #append this line (but now tag stays False)
elif tag: # if tag is true, then append that line
lines.append(f.readlines())
f.close()
The tag is reset on every iteration which means that it never outputs anything other than a line that has end text
in it.该标签在每次迭代时都会重置,这意味着它永远不会输出除包含
end text
的行之外的任何内容。 Also, some reordering of the operations is needed.此外,还需要对操作进行一些重新排序。
lines = []
for p in Doc1:
tag = False # Set the initial tag to False for the new file.
with open(p, 'r') as f:
for line in f:
if tag: # First line will never be printed since it must be a start tag
lines.append(line)
if line.startswith('start'): # Start adding from next line
tag = True
elif 'end' in line: # Already added the line so we can reset the tag
tag = False
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.