简体   繁体   中英

How to read text file and save specific words into csv or another text file using python

I have a text file which look like:

<URProgram createdIn="3.0.0.0" lastSavedIn="3.0.0.0" robotSerialNumber="" name="fix_test" directory="" installation="default">
  <children>
    <MainProgram runOnlyOnce="true">
      <children>
        <Script type="File">
          <cachedContents>
            #fix_test
            #Generated by Robotmaster

            Frame_0 = p[0.000000, 0.000000, 0.000000, 0.000000, 0.000000, 0.000000]
            set_tcp(p[0.000000, 0.149000, 0.098500, -1.209200, -1.209200, -1.209200])
            movej([0.000000, -1.570796, -1.570796, 3.141593, -1.570796, 3.141593], a=0.5236, v=0.5236, r=0.002)
            movej(get_inverse_kin(pose_trans(Frame_0, p[0.610000, 0.021012, 0.255841, 2.356194, 0.000000, 0.000000]), [0.676328, -1.397491, -2.205785, 3.603275, -0.894469, 2.356194]), a=0.5236, v=0.5236, r=0.002)
            movej(get_inverse_kin(pose_trans(Frame_0, p[0.610000, 0.013941, 0.248770, 2.356194, 0.000000, 0.000000]), [0.662638, -1.400102, -2.217578, 3.617680, -0.908158, 2.356194]), a=0.5236, v=0.5236, r=0.002)
            movej(get_inverse_kin(pose_trans(Frame_0, p[0.610000, -0.017879, 0.216950, 2.356194, 0.000000, 0.000000]), [0.598347, -1.417498, -2.267414, 3.684913, -0.972450, 2.356194]), a=0.5236, v=0.5236, r=0.002)
            movel(pose_trans(Frame_0, p[0.610000, -0.023536, 0.211293, 2.356194, 0.000000, 0.000000]), a=0.1, v=0.05, r=0)
            movel(pose_trans(Frame_0, p[0.600000, -0.023536, 0.211293, 2.356194, 0.000000, 0.000000]), a=0.1, v=0.05, r=0.002)
            movel(pose_trans(Frame_0, p[0.550000, -0.023536, 0.211293, 2.356194, 0.000000, 0.000000]), a=0.1, v=0.05, r=0.002)
            movel(pose_trans(Frame_0, p[0.540000, -0.023536, 0.211293, 2.356194, 0.000000, 0.000000]), a=0.1, v=0.05, r=0)
            movel(pose_trans(Frame_0, p[0.540000, -0.017879, 0.216950, 2.356194, 0.000000, 0.000000]), a=0.1, v=0.05, r=0.002)
            movej(get_inverse_kin(pose_trans(Frame_0, p[0.540000, 0.013941, 0.248770, 2.356194, 0.000000, 0.000000]), [0.789221, -1.260062, -2.317786, 3.577848, -0.781575, 2.356194]), a=0.5236, v=0.5236, r=0.002)
            movej(get_inverse_kin(pose_trans(Frame_0, p[0.540000, 0.021012, 0.255841, 2.356194, 0.000000, 0.000000]), [0.804109, -1.258814, -2.304978, 3.563793, -0.766687, 2.356194]), a=0.5236, v=0.5236, r=0.002)
            movej([0.000000, -1.570796, -1.570796, 3.141593, -1.570796, 3.141593], a=0.5236, v=0.5236, r=0.002)
          </cachedContents>
          <file>20201027_185454-fix_test.script</file>
        </Script>
      </children>
    </MainProgram>
  </children>
</URProgram>

and I would like to save it as follow:

p[0.610000, 0.021012, 0.255841, 2.356194, 0.000000, 0.000000]

p[0.610000, 0.013941, 0.248770, 2.356194, 0.000000, 0.000000]

p[0.610000, -0.017879, 0.216950, 2.356194, 0.000000, 0.000000]

p[0.610000, -0.023536, 0.211293, 2.356194, 0.000000, 0.000000]

p[0.600000, -0.023536, 0.211293, 2.356194, 0.000000, 0.000000]

p[0.550000, -0.023536, 0.211293, 2.356194, 0.000000, 0.000000]

p[0.540000, -0.023536, 0.211293, 2.356194, 0.000000, 0.000000]

p[0.540000, -0.017879, 0.216950, 2.356194, 0.000000, 0.000000]

What I need is the TCP position at the middle of every "movel" line, how can I do it with python code? I know how to read the txt file and split it into line, but I don't know how to extract specific words. Please help me!

If you can read the file and split it into lines then you only lack the proper regex which is:

movel.+(p\[([+-]?(\d*[.])?\d+(, )?)+\])

Note: The part [+-]?(\d*[.])?\d+ matches any floating point number (stolen from here ).

See my saved RegExr for detailed explanation.

Full example:

regex = r"movel.+(p\[([+-]?(\d*[.])?\d+(, )?)+\])"
for line in file.splitlines():
    match = re.match(regex, line.strip())
    if match:
        print(match.group(1))

# Output:
# p[0.610000, -0.023536, 0.211293, 2.356194, 0.000000, 0.000000]
# p[0.600000, -0.023536, 0.211293, 2.356194, 0.000000, 0.000000]
# p[0.550000, -0.023536, 0.211293, 2.356194, 0.000000, 0.000000]
# p[0.540000, -0.023536, 0.211293, 2.356194, 0.000000, 0.000000]
# p[0.540000, -0.017879, 0.216950, 2.356194, 0.000000, 0.000000]

Feel free to store the output or write it to a file instead of printing it.

Here another solution, with a less complex regex but less robust too.

Looking at your file, the only occurence of p[...] are the ones you want to extract

so p\[.*?\] matches anything starting with p[ until the next ]

import re


FILENAME = "a_file.txt"
pattern = re.compile("p\[.*?\]")


with open(FILENAME,'r') as  file:
    lines = [line.rstrip('\n') for line in file]

for line in lines:
    result=pattern.search(line)

    # if no pattern was found, search outputs None

    if result is not None : 

        print(result.group(0))


#p[0.000000, 0.000000, 0.000000, 0.000000, 0.000000, 0.000000]
#p[0.000000, 0.149000, 0.098500, -1.209200, -1.209200, -1.209200]
#p[0.610000, 0.021012, 0.255841, 2.356194, 0.000000, 0.000000]
#p[0.610000, 0.013941, 0.248770, 2.356194, 0.000000, 0.000000]
#p[0.610000, -0.017879, 0.216950, 2.356194, 0.000000, 0.000000]
#p[0.610000, -0.023536, 0.211293, 2.356194, 0.000000, 0.000000]
#p[0.600000, -0.023536, 0.211293, 2.356194, 0.000000, 0.000000]
#p[0.550000, -0.023536, 0.211293, 2.356194, 0.000000, 0.000000]
#p[0.540000, -0.023536, 0.211293, 2.356194, 0.000000, 0.000000]
#p[0.540000, -0.017879, 0.216950, 2.356194, 0.000000, 0.000000]
#p[0.540000, 0.013941, 0.248770, 2.356194, 0.000000, 0.000000]
#p[0.540000, 0.021012, 0.255841, 2.356194, 0.000000, 0.000000]


The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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