从文件追加特定行,在 For 循环内打开每个文件
Appending Specific Lines from a File, inside For Loop to open each file
我有 4 个装满 txt 文档的文件夹。我使用下面的代码提取所有 txt 并将它们附加到列表中。
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()
上面的这段代码工作得很好。然而,现在我想做的是:
- 对于文件夹中的每个 txt 文档,我只想在开始和结束之间附加行,以删除不必要的文本。所有文件夹中的每个文档的开始和结束文本都是相同的。我尝试这样做:
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()
此代码运行时,我没有收到任何警告或错误。但是没有行附加到行。 TIA 寻求任何建议和帮助。
这是因为您使用的是 line
,是字符串形式的文件,
未读,要做到这一点,你可以这样做:
for line in f:
# write code here
所以你的代码变成这样:
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()
标签在每次迭代时都会被重置,这意味着除了其中包含 end text
的行之外,它不会输出任何内容。此外,需要对操作进行一些重新排序。
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
我有 4 个装满 txt 文档的文件夹。我使用下面的代码提取所有 txt 并将它们附加到列表中。
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()
上面的这段代码工作得很好。然而,现在我想做的是:
- 对于文件夹中的每个 txt 文档,我只想在开始和结束之间附加行,以删除不必要的文本。所有文件夹中的每个文档的开始和结束文本都是相同的。我尝试这样做:
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()
此代码运行时,我没有收到任何警告或错误。但是没有行附加到行。 TIA 寻求任何建议和帮助。
这是因为您使用的是 line
,是字符串形式的文件,
未读,要做到这一点,你可以这样做:
for line in f:
# write code here
所以你的代码变成这样:
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()
标签在每次迭代时都会被重置,这意味着除了其中包含 end text
的行之外,它不会输出任何内容。此外,需要对操作进行一些重新排序。
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