匹配一行后,如何读取更多行并记录值,然后重新开始? Python
我想知道如何在文本文档中搜索单词 POLYLINE,然后一旦找到它,如何继续在文本文档中搜索 POLYLINE 的更多属性,例如 x 坐标和 y 坐标,然后找到下一个 POLYLINE 并执行此操作再次。
我有一个如下所示的文本文件:
- POLYLINE
- blah
- X coord
- 50
- Y coord
- 63
- blah
- blah
- X coord
- 90
- Y coord
- 6
- POLYLINE
- 依此类推...到目前为止,
我的所有代码所做的就是找到单词 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:
- POLYLINE
- blah
- X coord
- fifty
- Y coord
- sixty three
- blah
- blah
- X coord
- ninety
- Y coord
- six
- POLYLINE
- 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
至于你如何真正找到坐标,我们很难用你提供的信息做任何事情。如果没有关于坐标将出现在哪一行的模式,或者如果有其他数字不是您的坐标,并且这些数字没有某种识别本身,那么您无能为力。基本上,找到任何可以让您将坐标与其他任何东西区分开来的东西,然后搜索它。
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.
假设结构是一致的,您可以收集如下属性。
Assuming the structure is consistent you could collect the attributes like below.