匹配一行后,如何读取更多行并记录值,然后重新开始? Python

发布于 2025-01-04 01:30:09 字数 633 浏览 1 评论 0原文

我想知道如何在文本文档中搜索单词 POLYLINE,然后一旦找到它,如何继续在文本文档中搜索 POLYLINE 的更多属性,例如 x 坐标和 y 坐标,然后找到下一个 POLYLINE 并执行此操作再次。

我有一个如下所示的文本文件:

  1. POLYLINE
  2. blah
  3. X coord
  4. 50
  5. Y coord
  6. 63
  7. blah
  8. blah
  9. X coord
  10. 90
  11. Y coord
  12. 6
  13. POLYLINE
  14. 依此类推...到目前为止,

我的所有代码所做的就是找到单词 POLYLINE,我一直在尝试收集 POLYLINE 的属性。 这是到目前为止我的代码:

import re

fileName = open("textdoc.txt, "r")



for line in fileName:
    if re.match("POLYLINE", line):
        print line



fileName.close()

我该如何解决这个问题?

I am wondering how to search a text document for the word POLYLINE, and then once I find it, how to keep searching the text document for more attributes for POLYLINE, like x coordinates and y coordinates, and then find the next POLYLINE and do it again.

I have a text file that looks like this:

  1. POLYLINE
  2. blah
  3. X coord
  4. fifty
  5. Y coord
  6. sixty three
  7. blah
  8. blah
  9. X coord
  10. ninety
  11. Y coord
  12. six
  13. POLYLINE
  14. And so on...

All my code does so far is find the word POLYLINE, I am stuck trying to collect the attributes of POLYLINE.
Here is my code so far:

import re

fileName = open("textdoc.txt, "r")



for line in fileName:
    if re.match("POLYLINE", line):
        print line



fileName.close()

How can I fix this?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

栩栩如生 2025-01-11 01:30:09
for line in fileName:
    if re.match("POLYLINE", line):
        for line in filename:
            if re.match(xcoord,line):
                dostuff()
            if re.match(ycoord,line):
                dostuff()

至于你如何真正找到坐标,我们很难用你提供的信息做任何事情。如果没有关于坐标将出现在哪一行的模式,或者如果有其他数字不是您的坐标,并且这些数字没有某种识别本身,那么您无能为力。基本上,找到任何可以让您将坐标与其他任何东西区分开来的东西,然后搜索它。

for line in fileName:
    if re.match("POLYLINE", line):
        for line in filename:
            if re.match(xcoord,line):
                dostuff()
            if re.match(ycoord,line):
                dostuff()

As to how you could actually find the coordinates, it's hard for us to do anything with what you've provided. If there is no pattern as to what line the coordinates will appear in or if there are other numbers that are not your coordinates, and those numbers don't have some sort of identification as such, then there's not much you can do. Basically, find whatever it is that allows you to differentiate the coordinates from anything else, and then just search for that.

书信已泛黄 2025-01-11 01:30:09

假设结构是一致的,您可以收集如下属性。

#store polylines in a list for future use
polylines=[]

dataFile = open('textdoc.txt')

#collect the attributes in dictionaries
attrs={}

#because it appears you need to look one line ahead to get the coordinates
# it would be easiest to read all lines into a list

datalines = dataFile.readlines()
for idx, line in enumerate(datalines):
    #handle polyline flags by storing the previous attributes
    if 'POLYLINE' in line:
        #attrs will evaluate to True if its not empty
        if attrs:
            #append the old polyline attributes and start a new one
            polylines.append(attrs)
            attrs = {}

        continue

    #collect the attributes from the line following the coord flag
    # of course this breaks real fast if the file structure changes
    if 'xcoord' in line:
        #grab the coordinate from the following line
        attrs['xcoord'] = datalines[idx + 1].strip()
        continue

    if 'ycoord' in line:
        attrs['ycoord'] = datalines[idx + 1].strip()
        continue

Assuming the structure is consistent you could collect the attributes like below.

#store polylines in a list for future use
polylines=[]

dataFile = open('textdoc.txt')

#collect the attributes in dictionaries
attrs={}

#because it appears you need to look one line ahead to get the coordinates
# it would be easiest to read all lines into a list

datalines = dataFile.readlines()
for idx, line in enumerate(datalines):
    #handle polyline flags by storing the previous attributes
    if 'POLYLINE' in line:
        #attrs will evaluate to True if its not empty
        if attrs:
            #append the old polyline attributes and start a new one
            polylines.append(attrs)
            attrs = {}

        continue

    #collect the attributes from the line following the coord flag
    # of course this breaks real fast if the file structure changes
    if 'xcoord' in line:
        #grab the coordinate from the following line
        attrs['xcoord'] = datalines[idx + 1].strip()
        continue

    if 'ycoord' in line:
        attrs['ycoord'] = datalines[idx + 1].strip()
        continue
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文