简体   繁体   English

如何使用Python将文本文件中的某些文本行放入列表中?

[英]How can I put certain lines of text from a text file into a list with Python?

If I have hundreds of these in a text file; 如果我在一个文本文件中有数百个这样的文件;

<Vertex> 0 {
  -10.6272 8.71309 10.8633
  <UV> { 0.724203 0.210816 }
  <RGBA> { 0.916 0.609 0.439 1 }
}

How can I get Python to go through the text file and put the second line; 我如何让Python浏览文本文件并放入第二行; -10.6272 8.71309 10.8633 ; -10.6272 8.71309 10.8633 ; of each <Vertex> tag into a list? 每个<Vertex>标签放入列表中?

You can do it with a regular expression: 您可以使用正则表达式执行此操作:

>>> import re
>>> r = re.compile("^<Vertex>\s*\d+\s*{\s*([-\d. ]+)", re.MULTILINE)
>>> with open("filename") as fd:
>>>     matches = r.findall(fd.read())
>>> matches
['-10.6272 8.71309 10.8633', '-10.6272 8.71309 10.8633', ...]
catch = False
mylist = []
with open("myfile.txt", "r") as f:
    content = f.readlines()
for line in content:
    if line.startswith("<Vertex>"):
        catch = True
        continue
    if catch:
        catch = False
        mylist.append(line)

This should work. 这应该工作。

If you aren't worried about the consistency of the file, then it's pretty easy. 如果您不担心文件的一致性,那就很简单。

def readFile(path):
    f = open(path, 'r')
    return f.readlines()

def parseVertexes(lines):
    coordinates = []

    for index, line in enumerate(lines):
        if index % 5 == 1: #second line in vertex
            coordinates.append(line.split(" "))

I haven't fully tested, but that should work. 我还没有完全测试,但是应该可以。 You'll have to build up more infrastructure to handle cases if the file isn't consistent. 如果文件不一致,您将必须建立更多的基础结构来处理案件。

Assuming your file is like so: 假设您的文件是这样的:

<Vertex> 0 {
  -10.6272 8.71309 10.8633
  <UV> { 0.724203 0.210816 }
  <RGBA> { 0.916 0.609 0.439 1 }
}
<Vertex> 0 {
  -10.6272 8.71309 10.8633
  <UV> { 0.724203 0.210816 }
  <RGBA> { 0.916 0.609 0.439 1 }
}
<Vertex> 0 {
  -10.6272 8.71309 10.8633
  <UV> { 0.724203 0.210816 }
  <RGBA> { 0.916 0.609 0.439 1 }
}

Then you can just start on the second line, and pick every 5th line, using string slicing to get rid of the spaces at the start, and the new line character at the end. 然后,您可以从第二行开始,然后选择每5行,使用字符串切片在开头删除空格,在结尾删除新行字符。

file = open("file.txt","r")
mylist = []
for l,line in enumerate(file):
    if (l - 1) % 5 == 0:
        mylist.append(line[2:-2])

file.close()

Or, as a one liner: 或者,作为一个班轮:

[line[2:-2] for l,line in enumerate(open("file.txt","r")) if (l - 1) % 5 == 0]

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

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