Reading and writing specific content
I am trying to read the specific content of file1.txt and write that specific content to another file2.txt. The problem is that I read the whole part after Bar, where I want to read only the beginning of the line with [x] and only using the Bar section.
Source
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 would be greatly appreciated? Thank:)
source to share
First you need to determine if you are in the "Bar" block. Then, while you are, print / copy 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 the list returned by the function get_selected_block_entries()
. Similarly file2.txt
contains:
##### Bar: * [x] Email to A * [x] Email to C
This output indicates that the selected records following the "Bar:" block are not being collected.
Also note that the selected records will be collected from all matching blocks if there is more than one, for example.
get_selected_block_entries(infile, 'Foo')
will return selected records from two Foo blocks:
['##### Foo\n', '* [x] Clone 2\n', '##### Foo\n', '* [x] Clone 2\n']
And if you ever want to select all selected records from all blocks, you can do this:
get_selected_block_entries(infile, '')
source to share
Turn on and off found
by detecting partitions. When found
True
filtering rows with '[x]' in line
.
found = False
for line in open('file1.txt'):
line = line.strip()
if not line:
continue
if line.startswith('#####'):
if line == '##### Bar:':
found = True
print(line)
else:
if found:
break
continue
if found and '[x]' in line:
print(line)
source to share
You probably want to check if a given line starts with "* [x]"
.
import re
section = None
for line in data_file:
sre = re.match("^#####\s*(\w):\s*",line)
if sre:
section = sre.group(1)
if line.startswith("* [x]") and section == "Bar":
block += line
Take a look here for more information on using regular expressions in python.
source to share