2015-06-21 127 views
3

我正在嘗試讀取file1.txt的特定內容,並將此特定內容寫入另一個文件file2.txt中。問題是我讀完Bar後的所有部分,我只想讀取[x]開頭的行,並且只讀取Bar部分。讀取並寫入特定內容

源代碼

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() 

輸入文件 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 

任何建議將不勝感激?謝謝:)

Subsequent question

+0

請[edit]添加一個特定的問題陳述 - 「不起作用」可以假設,但* how *不起作用?什麼錯誤信息或不正確的行爲是特徵? –

+0

if'[x]'in line:? –

+0

是的。 [x]在行首。有些行是[],這不是我們的考慮因素。 – Roujri

回答

1

您首先需要檢測您是否在「Bar」塊內。然後,當你在時,打印/累積那些以* [x]開頭的行。下面是做這件事:

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) 

運行上面的代碼時file1.txt包含:

 
##### Foo 
* [] Task 1 
* [x] Clone 2 


##### Bar: 
* [x] Email to A 
* [] Email to B 
* [x] Email to C 

##### Foo 
* [] Task 1 
* [x] Clone 2 

打印:

 
['##### Bar:\n', '* [x] Email to A\n', '* [x] Email to C\n'] 

這是清單從get_selected_block_entries()函數返回。類似地file2.txt包含:

 
##### Bar: 
* [x] Email to A 
* [x] Email to C 

此輸出顯示選擇的以下內容的條目「欄:」塊不收集。

還要注意的是,如果有多於一個的話,所選的條目將從所有匹配的塊中收集。

get_selected_block_entries(infile, 'Foo')將從富塊返回選定條目:

['##### Foo\n', '* [x] Clone 2\n', '##### Foo\n', '* [x] Clone 2\n'] 

而且,如果你想從所有塊選擇所有選擇項,你可以這樣做:

get_selected_block_entries(infile, '') 
+0

優秀的解決方案。謝謝 :) – Roujri

1

你可能想測試一個給定的符合"* [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 

查看here瞭解更多關於在python中使用正則表達式的信息。

+0

謝謝。不過,我仍然無法獲得預期的產出。問題是我得到所有的線條後,塊塊開始與* [x]我想要在塊內 – Roujri

2

切換found通過檢測部分來打開和關閉。當foundTrue'[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) 
相關問題