如何在文件中的某一行之后将数据存储在数组中

时间:2018-02-13 21:54:42

标签: python file

我有一个我正在使用的文件就是它的样子:

import React from 'react'
import { Provider } from 'react-redux'
import { BrowserRouter } from 'react-router-dom'
import { configure, shallow, mount } from 'enzyme'
import configureMockStore from 'redux-mock-store'
import thunk from 'redux-thunk'

import 'datatables.net-se'
jest.mock('datatables.net-se', () => ({ dataTable: jest.fn() }))

// Components 
import Page from '../../components/Customers/Page'

// Setups
const middlewares = [thunk] // add your middlewares like `redux-thunk`
const mockStore = configureMockStore(middlewares)

let store, props, component, wrapper

describe("<Page />", function() {  

  beforeEach(()=>{
    const storeStateMock = {
      customers: Customers
    }

    store = mockStore(storeStateMock)

    props = {
      fetchCustomers: jest.fn()
    }

    wrapper = mount(<BrowserRouter><Provider store={store}><Page {...props} /></Provider></BrowserRouter>)
  })

  it('renders connected component', function() { 

    expect(wrapper.find(Page).length).toEqual(1)
  })

})

}

现在我试图将“v”之后的所有数字存储到verts []中。 这就是我目前所拥有的:

v
-1.00000000 -1.00000000 -1.00000000             
-1.00000000 1.00000000 -1.00000000            
-1.00000000 1.00000000 1.00000000          
-1.00000000 1.00000000 -1.00000000             
-1.00000000 1.00000000 1.00000000             
1.00000000 1.00000000 -1.00000000             
1.00000000 1.00000000 1.00000000             
1.00000000 -1.00000000 -1.00000000              
1.00000000 1.00000000 -1.00000000               
1.00000000 1.00000000 1.00000000             
-1.00000000 -1.00000000 -1.00000000            
-1.00000000 -1.00000000 1.00000000   
-1.00000000 -1.00000000 1.00000000      
1.00000000 -1.00000000 1.00000000             
-1.00000000 -1.00000000 -1.00000000          
1.00000000 -1.00000000 -1.00000000         
1.00000000 -1.00000000 1.00000000          
-1.00000000 -1.00000000 -1.00000000       
1.00000000 -1.00000000 -1.00000000         
1.00000000 1.00000000 -1.00000000            
-1.00000000 -1.00000000 -1.00000000          
-1.00000000 1.00000000 -1.00000000           
-1.00000000 1.00000000 -1.00000000        
1.00000000 1.00000000 -1.00000000          
-1.00000000 -1.00000000 1.00000000         
-1.00000000 1.00000000 1.00000000          
1.00000000 -1.00000000 1.00000000       
-1.00000000 1.00000000 1.00000000     
1.00000000 -1.00000000 1.00000000         
1.00000000 1.00000000 1.00000000       
-1.00000000 1.00000000 1.00000000       
1.00000000 -1.00000000 -1.00000000      
1.00000000 -1.00000000 1.00000000         
1.00000000 1.00000000 -1.00000000           
1.00000000 1.00000000 1.00000000         
-1.00000000 -1.00000000 1.00000000 

请记住,v下面的顶点数量会有所不同。我需要将它们全部存储在verts数组中。谢谢你的帮助!

1 个答案:

答案 0 :(得分:1)

如果v始终位于第一行,您可以执行以下操作:

with open('verts.txt') as f:
    lines = f.readlines()[1:]

verts = []
for line in lines:
    verts.append([float(p) for p in line.split()])

如果不是,您可以使用切片获取其下的行。

with open('verts.txt') as f:
    lines = f.readlines()

i = lines.index('v\n')
lines = lines[i + 1:]

verts = []
for line in lines:
    verts.append([float(p) for p in line.split()])

好的,我确实提出了一些更通用的东西,试图解析你拥有的文件(或者至少部分文件)。

def read_data(data, marker='v'):
    marker_index = data.index(marker)

    result = []

    for line in data[marker_index + 1:]:
        if not line:
            break
        result.append([float(n) for n in line.split()])

    return result

它从给定标记读取输入,直到找到空行。我已使用您提供的文件对其进行了测试,以解析vvNorms标记的内容。

with open('data.txt') as f:
    stripped_data = [l.strip() for l in f]

verts = read_data(stripped_data)

print(len(verts))
print(verts[0])
print(verts[-1])

v_norms = read_data(stripped_data, marker='vNorms')
print(len(v_norms))
print(v_norms[0])
print(v_norms[-1])

输出:

36
[-1.0, -1.0, -1.0]
[-1.0, -1.0, 1.0]

36
[-1.0, 0.0, 0.0]
[-1.0, 0.0, 0.0]