2015-01-15 146 views
0

我知道如何從文本文件中提取數字,這部分有所幫助。這是我的問題。我有一個文本文件,它看起來像:使用Python從文件中提取數字段?

Some crap here: 3434 
A couple more lines 
of crap. 
34 56 56 
34 55 55 
A bunch more crap here 
More crap here: 23 
And more: 33 
54 545 54 
4555 55 55 

我想寫提取與三個數字線的腳本,並把它們放在不同的文本文件。例如,我有一個文件:

34 56 56 
34 55 55 

而另一個文件:

54 545 54 
4555 55 55 

現在我有:

for line in file_in: 
    try: 
     float(line[1]) 
     file_out.write(line) 
    except ValueError: 
     print "Just using this as placeholder" 

這成功地把數塊都成一個單一的文件。但我需要它把一個塊放在一個文件中,另一個塊放在另一個文件中,而我卻不知道如何完成這個任務。

+0

有什麼具體的分離數字?或者僅僅是這些數字組需要被分組到不同的文件中?而不是使用'try/except'塊,你可以使用'str.isdigit()'來檢查一個字符串是否是一個數字。 – Scironic 2015-01-15 16:50:42

+0

不幸的是沒有具體分隔數字。儘管如此,文本中可能會有關鍵短語將數字分開。這是真的,數字組需要在不同的文件。 – user1566200 2015-01-15 16:52:32

回答

0

要知道,如果一個字符串是一個數字,你可以使用str.isdigit

for line in file_in: 
    # split line to parts 
    parts = line.strip().split() 
    # check all parts are numbers 
    if all([str.isdigit(part) for part in parts]): 
     if should_split: 
      split += 1 
      with open('split%d' % split, 'a') as f: 
       f.write(line) 
      # don't split until we skip a line 
      should_split = False 
     else: 
      with open('split%d' % split, 'a') as f: 
       f.write(line) 
    elif not should_split: 
     # skipped line means we should split 
     should_split = True 
0

你沒有指定你正在使用Python版本,但你可能會接近這種方式在Python2.7。

string.translate需要一個翻譯表(可以是None)和一組要翻譯的字符(或者如果table爲None,則可以將其刪除)。

>>> import string 
>>> remove_chars = string.printable[10:-6] + string.printable[-4:] 
>>> string.translate('Some crap 3434', None, remove_chars) 
' 3434' 
>>> string.translate('34 45 56', None, remove_chars) 
'34 45 56' 

添加strip修剪的左側和右側的空白並遍歷包含從數據testfile的:

您可以通過正確地切分string.printable設置你的delete_chars於一切,但0-9和空間你的問題:

>>> with open('testfile.txt') as testfile: 
... for line in testfile: 
...  trans = line.translate(None, remove_chars).strip() 
...  if trans: 
...  print trans 
... 
3434 
34 56 56 
34 55 55 
23 
33 
54 545 54 
4555 55 55 
0

您可以使用正則表達式here.But這將需要file.read()什麼讀文件到一個變量(如果文件不是很大)

((?:(?:\d+){2}\d+(?:\n|$))+) 

查看演示。

https://regex101.com/r/tX2bH4/20

import re 
p = re.compile(r'((?:(?:\d+){2}\d+(?:\n|$))+)', re.IGNORECASE) 
test_str = "Some crap here: 3434\nA couple more lines\nof crap.\n34 56 56\n34 55 55\nA bunch more crap here\nMore crap here: 23\nAnd more: 33\n54 545 54\n4555 55 55" 

re.findall(p, test_str) 

re.findall返回list.You可以很容易地把列表中的每個內容的新文件。