Python:在文件中搜索,替换前面的条目

时间:2015-08-25 14:11:07

标签: python replace

我正在尝试以特定方式更改现有的ASCII数据文件。 我想要的方法是找到一个数组中的一个字符串,我事先定义。 如果在文件中找到此字符串,我想更改前面的条目;放在这里的字符串取决于首先找到哪个字符串。

我有一个文件,其中的entrys由空格分隔,并且我在末尾有尾随空格以填充30列。相应的字符串不在第一行,每行不会超过一个。一个例子可能如下所示:

test01out.txt:

a0997 b0998 c0999            
a1000 b1001 c1002            
a1003 b1004 c1005            
a1006 a1000 c1007            
a1008 b1009 c1010            
b1001 b1011 c1012            
a1013 b1014 b1001            
a1015 b1016 c1017            

该文件不一定必须连续有三列。有可能,一行只有两行,但也可能有四到五列。

我目前的尝试如下:

from numpy import *

findlines = open("test01.txt").read().split("\n")
searcharray = array(["a1000","b1001"])
alterarray  = array(["this1","this2"])

tempstring_current = ""
fileout = open("test01out.txt", "w")

for i, line in enumerate(findlines):
    tempstring_last = tempstring_current
    tempstring_current = line.rstrip().split(" "))
    if any(x in tempstring_current for x in searcharray):               # check if one of the elements is in the current line -> unfortunately this seems to be true for any line checked...
        print(i)
        print(tempstring_current)



        for j, element in enumerate(tempstring_current):
            if any(searcharray == tempstring_current):
                currentsearchindex = argmax(searcharray == tempstring_current)

        currentalterstring = alterarray[currentsearchindex]
        if currentsearchindex == 0:
            tempstring_last.split(" ")[-1] = currentalterstring
        else:
            tempstring_current.split(" ")[currentsearchindex - 1] = currentalterstring

        tempstring_current.split(" ")[currentsearchindex-1] = "XPRZeugs_towrite" + repr(currentdesignatedspeed)

    tempstring_last = tempstring_last.ljust(30)
    try: 
        fileout.write(str(tempstring_last))
        fileout.write("\r")
try: 
    fileout.close()

searcharray和alterarray会有两个以上的元素。 我已经测试了脚本到任何条件;不幸的是,任何条件似乎总是因为某些原因而得到满足我不太明白:

from numpy import *

findlines = open("test01.txt").read().split("\n")
searcharray = array(["a1000","b1001"])
alterarray  = array(["this1","this2"])

tempstring_current = ""
fileout = open("test01out.txt", "w")

for i, line in enumerate(findlines):
    tempstring_last = tempstring_current
    tempstring_current = line.rstrip().split(" ")
    if any(x in tempstring_current for x in searcharray):               # check if one of the elements is in the current line -> unfortunately this seems to be true for any line checked...
        print(i)
        print(tempstring_current)

我为文件中的每一行打印了这些行,这是我没想到的......

我非常感谢任何建议。

谢谢!

修改/解决方案:

我意识到我在输入测试文件中犯了一个错误: 它应该是这样的:

a0997 b0998 c0999            
a1000 b1001 c1001            
a1003 b1004 c1005            
a1006 a1000 c1007            
a1008 b1009 c1010            
c1002 b1011 c1012            
a1013 b1014 c1002            
a1015 b1016 c1017            

完成工作的完整代码如下:

from numpy import *

findlines = open("test01.txt").read().split("\n")
searcharray = array(["a1000","c1002"])
alterarray  = array(["this1","this2"])

tempstring_current = ""
fileout = open("test01out.txt", "w")

for i, line in enumerate(findlines):
    tempstring_last = tempstring_current
    tempstring_current = line.rstrip().split(" ")
    if any([x in tempstring_current for x in searcharray]):               # check if one of the elements is in the current line -> unfortunately this seems to be true for any line checked...
        # print(i)
        # print(tempstring_current)
        # print(searcharray)
        # print([x in tempstring_current for x in searcharray])
        # print(argmax([x in tempstring_current for x in searcharray]))
        currentsearchposindex = argmax([x in tempstring_current for x in searcharray])      # welchen Index hat das entsprechende Element im Searcharray?

        currentalterstring = alterarray[currentsearchposindex]                              # was ist der entsprechende Eintrag im Alterarray




        for j, currentXPRelement in enumerate(tempstring_current):
            if currentXPRelement == searcharray[currentsearchposindex]:
                currentsearchindex_intemparray = j

        # print(len(tempstring_current))
        # print(searcharray[currentsearchposindex])
        # print(tempstring_current == searcharray[currentsearchposindex])
        # print(searcharray[currentsearchposindex] == tempstring_current)
        # print(argmax(tempstring_current == searcharray[currentsearchposindex]))
        # currentsearchindex_intemparray = argmax(tempstring_current == searcharray[currentsearchposindex])

        if currentsearchindex_intemparray == 0:
            tempstring_last[-1] = currentalterstring
        else:
            tempstring_current[currentsearchindex_intemparray - 1] = currentalterstring

        # tempstring_current[currentsearchindex_intemparray-1] = "XPRZeugs_towrite" + repr(currentalterstring)

    tempstring_last = str(" ".join(tempstring_last)).ljust(30)
    if not i == 0:
        try: 
            fileout.write(str(tempstring_last))
            fileout.write("\r")
        finally:
            None   


try: 
    fileout.write(" ".join(tempstring_current))
    fileout.write("\r")
    fileout.close()
finally:
    None   

感谢您的回答!

1 个答案:

答案 0 :(得分:0)

要修复您的代码,至少它可能无法始终匹配,请更改

if any(x in tempstring_current for x in searcharray):

if any([x in tempstring_current for x in searcharray]):

我认为原因是searcharray中x的tempstring_current中的'' expression返回一个interator函数 - any()表示'这个值(即迭代器函数引用)不是None,因此它是True',因此结果始终为True。更改的语法从迭代器创建一个列表,然后任何可能的工作,即如果列表中的任何元素为真,则返回true。