读取和写入特定的内容 [英] Read and Write the specific content
问题描述
我正在尝试读取file1.txt的特定内容,并将此特定内容写入另一个文件file2.txt中。问题是我读完Bar后的所有部分,我只想用[x]和Bar部分读取行开始。
源代码
$ b $ pre $
def read_write_file_content():
data_file = open('file1.txt')
block =
found = False
为data_file中的行:
如果找到:
如果line.strip()==##### Foo:
break
else:
block + = line
else:
如果line.strip()==##### Bar::
found = True
block = line
print block
$ b data_file.close()
view_today ()
输入文件
file1.txt
##### Xyz
* []任务112
* [] Cl 221
##### Foo
* []任务1
* [x]克隆2
##### Bar:
* [x]电邮至A
* []电子邮件到B
* [x]电子邮件到C
#####鲍勃
* []任务3
* [x]克隆鲍勃
输出文件
file2.txt
#####酒吧:
* [x]电邮至A
* [x]电邮至C
任何建议都将不胜感激?谢谢:)
您首先需要检测您是否在Bar块内。然后,当你打印/累积以 * [x]
开头的行时。这里有一个方法:
$ p $ def get_selected_block_entries(lines,block_name,
block_prefix ='##### ',selected_entry_prefix ='* [x]'):
selected_lines = []
block_marker ='{} {}'。格式(block_prefix,block_name)
for line in行:
如果line.startswith(block_prefix):
in_block = line.startswith(block_marker)
如果in_block:
selected_lines.append(行)
else:
如果in_block和line.startswith(selected_entry_prefix):
selected_lines.append(line)
返回selected_lines
打开('file1.txt')作为infile,打开('file2.txt','w')作为outfile:
selected = get_selected_block_entries(infile,'Bar:')
打印selected#bar中的选定条目列表:block
outfile.writelines(选择)
当 file1.txt
包含以下代码时:
##克隆2
$ b ##### Bar
* [x]
$ [*]任务1
* [x]电邮至A
* []电邮至B
* [x]电邮至C
##### Foo
* []任务1
* [x]克隆2
打印:
['##### Bar:\ n','* [x] Email to A\\\
','* [x] Email to C\\\
']
它是从 get_selected_block_entries()
函数返回的列表。同样, file2.txt
包含:
##### Bar:
* [x]电子邮件至A
* [x]电子邮件至C
此输出显示Bar:块不会被收集。
另外请注意,如果有多个匹配的块,所有匹配的块将被收集,例如
$ p $ get_selected_block_entries(infile,'Foo')将从两个 Foo块返回所选的条目:
['##### Foo\\\
','* [x] Clone 2 \\\
','## ### Foo\\\
','* [x] Clone 2 \\\
']
而且,如果您想要从所有块中选择 all 所有块,您可以这样做:
get_selected_block_entries(infile,'')
I am trying to read specific content of a file1.txt and write this specific content in another file file2.txt. The problem is I read entire part after Bar where I want to read only the line start with [x] and only with the Bar section.
Source Code
def read_write_file_content():
data_file = open('file1.txt')
block = ""
found = False
for line in data_file:
if found:
if line.strip() == "##### Foo":
break
else:
block += line
else:
if line.strip() == "##### Bar:":
found = True
block = line
print block
data_file.close()
view_today()
Input file file1.txt
##### Xyz
* [] Task 112
* [] Cl 221
##### Foo
* [] Task 1
* [x] Clone 2
##### Bar:
* [x] Email to A
* [] Email to B
* [x] Email to C
##### Bob
* [] Task 3
* [x] Clone Bob
Outputfile file2.txt
##### Bar:
* [x] Email to A
* [x] Email to C
Any suggestion will be greatly appreciate ? Thanks :)
You first need to detect whether you are inside a "Bar" block. Then, while you are, print/accumulate those lines that start with * [x]
. Here's one way to do it:
def get_selected_block_entries(lines, block_name,
block_prefix='#####', selected_entry_prefix='* [x]'):
selected_lines = []
block_marker = '{} {}'.format(block_prefix, block_name)
for line in lines:
if line.startswith(block_prefix):
in_block = line.startswith(block_marker)
if in_block:
selected_lines.append(line)
else:
if in_block and line.startswith(selected_entry_prefix):
selected_lines.append(line)
return selected_lines
with open('file1.txt') as infile, open('file2.txt', 'w') as outfile:
selected = get_selected_block_entries(infile, 'Bar:')
print selected # a list of selected entries within a Bar: block
outfile.writelines(selected)
Running the above code when file1.txt
contains:
##### Foo * [] Task 1 * [x] Clone 2 ##### Bar: * [x] Email to A * [] Email to B * [x] Email to C ##### Foo * [] Task 1 * [x] Clone 2
prints:
['##### Bar:\n', '* [x] Email to A\n', '* [x] Email to C\n']
which is list returned from the get_selected_block_entries()
function. Similarly file2.txt
contains:
##### Bar: * [x] Email to A * [x] Email to C
This output shows that selected entries following a "Bar:" block are not collected.
Also note that selected entries will be collected from all matching blocks if there is more than one, e.g.
get_selected_block_entries(infile, 'Foo')
will return the selected entries from the two Foo blocks:
['##### Foo\n', '* [x] Clone 2\n', '##### Foo\n', '* [x] Clone 2\n']
And, if you ever wanted to select all selected entries from all blocks you could do this:
get_selected_block_entries(infile, '')
这篇关于读取和写入特定的内容的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!