简体   繁体   English

搜索文件中的特定字符串,然后将后续字符串保存在python文件中

[英]Searching a specific string in a file and save the string coming after in a file in python

I want to save some speicific data from files I have, it looks like something like that : 我想从已有的文件中保存一些特定的数据,看起来像这样:

[StagePositionMapFile]
[StageMapPosition]
XCoordinate=-1.37525e-003
YCoordinate=-5.52599999999999e-003
ZCoordinate=9.96477272727273e-003
RCoordinate=-2.18201293271677
TCoordinate=-1.19239958528248e-004
PositionName=Position 14

[StageMapPosition]
XCoordinate=-1.40625e-003
YCoordinate=-5.01925000000001e-003
ZCoordinate=9.96477272727273e-003
RCoordinate=-2.18201293271677
TCoordinate=-9.31942430672605e-005
PositionName=Position 13

[StageMapPosition]
XCoordinate=-1.387625e-003
YCoordinate=-4.68562500000001e-003
ZCoordinate=9.96477272727273e-003
RCoordinate=-2.18201293271677
TCoordinate=-9.31942430672605e-005
PositionName=Position 12 

I just want to keep the data after XCoordinate and YCoordinate, so what I did is to read the file and split it according to the equal sign then the lign return as you can see here on my code : 我只想将数据保留在XCoordinate和YCoordinate之后,所以我要做的是读取文件并根据等号将其拆分,然后按回车符进行修改,就像您在我的代码中看到的那样:

   with open("E:/WOrk/PHD/SEM_images/NS65/Systematic_study/100_uM /NS_65_6_K_15_mM_100_uM_B4_66%_60_s/positionw.txt","r") as openfile:



    for line in openfile :


        s=line.split("=")


        for index,line in enumerate(s) :


            list=[index,line]
            list2=line.split("\n")

However I don't really understand what is hapenning in the line.split("\\n") because i only get a list with the values after the = sign and i am not getting the XCoordinate string in the list. 但是我真的不明白line.split(“ \\ n”)中发生了什么,因为我只得到一个列表,其中包含=号后的值,而我没有在列表中获得XCoordinate字符串。

Second thing is that then i want to say when XCoordinate is found in the list take the string after. 第二件事是,我想说的是,在列表中找到XCoordinate时,将字符串放在后面。 I know i can make that using enumerate and the index. 我知道我可以使用枚举和索引。 But when I am just trying to print true if XCoordinate is in there it doesn't print anything... 但是当我只是尝试在其中显示XCoordinate时打印true时,它什么也不会打印...

In the end I want something like that : 最后,我想要这样的东西:

(values of XCoordinate)         (values of YCoordinate)
-1.37525e-003                    -5.52599999999999e-003

EDIT : 编辑:

I changed my code and made a list of all the words in my file text : 我更改了代码,并列出了文件文本中所有单词的列表:

with open("E:/WOrk/PHD/SEM_images/NS65/Systematic_study/100_uM/NS_65_6_K_15_mM_100_uM_B4_66%_60_s/positionw.txt","r") as openfile:


        separate=list()
        for line in openfile :


            s=line.split("=")


            for element in s :

                element2=element.split("\n")

                for value in element2: 


                    separate.append(value)

        for index,valeur in enumerate(separate) :

            chaine="XCoordinate"
            print(valeur)
            print("stop")
            if chaine in valeur :

                print("true")

Now I should get true everytime XCoordinate is showing up however it is not the case :/ 现在每次XCoordinate出现时我都应该成为现实,但事实并非如此:/

Thanks for your help. 谢谢你的帮助。

Mathais 马泰斯

I thinks this script will work for you 我认为该脚本将为您工作

text=''
    regx =re.compile('((XCoordinate=[-]{0,1}[0-9]*.[0-9]*e-[0-9]*)\n(YCoordinate=[-]{0,1}[0-9]*.[0-9]*e-[0-9]*))')
    with open('test.txt', 'r+') as file:
        for line in file:
            text += line

    find = re.findall(regx,text)
    print ('findaa  ', find)

and the out will be 然后将是

[('XCoordinate=-1.37525e-003\\nYCoordinate=-5.52599999999999e-003', 'XCoordinate=-1.37525e-003', 'YCoordinate=-5.52599999999999e-003'), ('XCoordinate=-1.40625e-003\\nYCoordinate=-5.01925000000001e-003', 'XCoordinate=-1.40625e-003', 'YCoordinate=-5.01925000000001e-003'), ('XCoordinate=1.387625e-003\\nYCoordinate=-4.68562500000001e-003', 'XCoordinate=1.387625e-003', 'YCoordinate=-4.68562500000001e-003')] [('XCoordinate = -1.37525e-003 \\ nYCoordinate = -5.52599999999999e-003','XCoordinate = -1.37525e-003','YCoordinate = -5.52599999999999e-003'),('XCoordinate = -1.40625e-003 \\ nYCoordinate = -5.01925000000001e-003','XCoordinate = -1.40625e-003','YCoordinate = -5.01925000000001e-003'),('XCoordinate = 1.387625e-003 \\ nYCoordinate = -4.68562500000001e-003',' XCoordinate = 1.387625e-003','YCoordinate = -4.68562500000001e-003')]

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM