删除文本文件中的所有额外空白区域并拔出strings-python

时间:2015-09-25 13:59:11

标签: python

我有多个文本文件,其中我需要提取两个单独的值,这些值在每个文件中是相同的行。值是整数但长度会发生变化。以下是我到目前为止的情况。

for file in glob.glob(os.path.join('*_test.txt')):
hostname = file.split('_')[0]
with open(file) as data:

    for line in data:
        removed = line.strip()
        if removed:
            if line.startswith("test"):
                words= ''.join(removed[6:])
                print words

输出目前是以下

       3946207263      1904562885
        365872669       106578501
         205088500       25576835

我的问题是删除左侧和值之间的所有额外空白区域的最佳方法是什么。我也陷入困境,因为值会改变长度,那么如果长度变化,那么在每条线上拉出两个值的最佳方法是什么?

1 个答案:

答案 0 :(得分:1)

正式表达救援。

import re
RX_MANY_SPACES = re.compile('\s+')  # one or more spaces
# substitute many spaces with one
assert RX_MANY_SPACES.sub(' ', 'a    b       c') == 'a b c'

改变口味。