消除与指定字符串相同的行中找到的字符串中的空格

时间:2013-12-28 06:44:40

标签: python string search replace position

我是Python的新手,这将是我的第一个编码。我看到了与我试图解决的问题有类似问题的主题,但由于我是新手,我无法从这些主题中找出去哪里(热点以适应我需要的东西)所以我抓住机会写我的问题如下。

问题是:我有一个包含多个匹配项的文本文件,如下所示。在发生时,>之前的字符。不要从一个事件更改为另一个事件而其他字符更改:

  

STMTTRN>

     

TRNTYPE>DÉBIT

     

DTPOSTED> 20 131 116 170 000

     

TRNAMT> -8,63

     

FITID> 201311181652559

     

NAME> LA FABRIQUE ARHOMA MONTREAL QC H

     

备忘录> 5719:LA FABRIQUE ARHOMA MONTREAL QC H2K1T4 CAN

     

/ STMTTRN>“中

基本上,我想消除<DTPOSTED>出现的行上数字之间的空格。我怎么能这样做?

我有一个基本的算法,但我真的不知道如何用Python编写它。我认为解决方案是:

  

在文档结束之前,请执行以下操作:找到位置   (“行和列”)“”在同一行和单词后面   “”,擦除所有空格字符,直到你读入一个(/ n)   标志传递到下一行

有人能帮助我吗?

2 个答案:

答案 0 :(得分:0)

示例输入文件inp.txt包含:

STMTTRN>
TRNTYPE>DEBIT
DTPOSTED>20 131 116 170 000
TRNAMT>-8,63
FITID>201311181652559
NAME>LA FABRIQUE ARHOMA MONTREAL QC H
MEMO>5719: LA FABRIQUE ARHOMA MONTREAL QC H2K1T4 CAN
/STMTTRN>"

STMTTRN>
TRNTYPE>DEBIT
DTPOSTED>20 131 116 170 000
TRNAMT>-8,63
FITID>201311181652559
NAME>LA FABRIQUE ARHOMA MONTREAL QC H
MEMO>5719: LA FABRIQUE ARHOMA MONTREAL QC H2K1T4 CAN
/STMTTRN>"

守则是,

def eliminate():
    ## read From the Text file.
    with open('inp.txt', 'r') as fp:
        ## Read the first line.
        ## Since the size of the file is not known
        ## reading the file line by line.
        line = fp.readline()

        while line:
            ## if the line is not Empty.
            if line:
                ## if "DTPOSTED" found in the line read.
                if 'dtposted' in line.lower():
                    ## Get the integer part alone.
                    _, value = line.split('>')
                    ## Remove the spaces between the integer value.
                    ## and print it to the console.
                    print ''.join(value.split())

            ## Reading the next line from the file.
            line = fp.readline()


eliminate()

OUTPUT是,

20131116170000
20131116170000

答案 1 :(得分:0)

#1 - open the file, get the content as a unique string including \n 
with open("inp.txt", "r") as f:
    a = "".join(f.readlines())

#2 - extract the numbers after keyword "DTPOSTED>"
numbers = a.split('DTPOSTED>')[-1].split('\n')[0]

#3 - save the parts of the file comming before and after DTPOSTED>
before, after = a.split(numbers)

#4 - remove spaces
numbers = "".join(numbers.split()).strip()

#5 - write output
with open("inp.modified.txt", "w") as f:
    f.write(before + numbers + after)