简体   繁体   English

如何使用 python 读取文本文件并将特定单词保存到 csv 或另一个文本文件中

[英]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.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.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.017879, 0.216950, 2.356194, 0.000000, 0.000000]

p[0.610000, -0.023536, 0.211293, 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.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.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.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.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?我需要的是每个“movel”行中间的 TCP position,我如何使用 python 代码来做到这一点? I know how to read the txt file and split it into line, but I don't know how to extract specific words.我知道如何读取 txt 文件并将其拆分为一行,但我不知道如何提取特定的单词。 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 ).注意:部分[+-]?(\d*[.])?\d+匹配任何浮点数(从这里窃取)。

See my saved RegExr for detailed explanation.有关详细说明,请参阅我保存的 RegExr

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.随意存储 output 或将其写入文件而不是打印。

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查看您的文件, p[...]的唯一出现是您要提取的文件

so p\[.*?\] matches anything starting with p[ until the next ]所以p\[.*?\]匹配任何以p[开始直到下一个]

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]


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

相关问题 如何使用 Python 读取文本文件中的特定单词 - How to read specific words in text file using Python 如何使用python将波斯语文本保存在csv文件中? - How to save Farsi text in csv file using python? Python:读取CSV文件并写入另一个文本文件 - Python: Read CSV file and write to another text file 如何使用python将包含特定单词的整行excel(.csv)复制到另一个csv文件中? - How to copy entire row of excel (.csv) which contain specific words into another csv file using python? 我需要读取文本文件并使用 python 将特定值插入到 csv 文件中 - I need to read a text file and insert specific values to csv file using python 如何使用 python 更快地搜索和读取特定文件夹中的文本文件 - How to search and read a text file in a specific folder faster using python 如何使用python从文本文件的行中读取特定字符? - How to read specific characters from lines in a text file using python? 如何从文本文件中获取两个特定单词之间的所有单词,然后使用python将其写入新的文本文件中 - How to get all the words between two specific words from a text file and write it in a new text file using python Python-如何读取文本文件中的特定行? - Python - How to read a specific line in a text file? 读取并处理文本文件并保存到csv - Read and process a text file and save to csv
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM