user8953650
user8953650

Reputation: 1020

Python : read a data set file

I have to read and store a data file composed of 3 columns and N row *Number of block .. here a simple example:

z=3 
1 2 3 
2 5 6 
1 5 7

z=2
1 3 9
2 5 7

which is the best way to read this file and store several block of z row using python ?

EDIT: I have to store them into a separate blocks (indexed) for example in the above example I should have

block[0]=[[1 2 3],[2 5 6],[1 5 7]] 
block[1] = [[1 3 9],[2 57]]   

@shahaf e come lo indicizzo ? scusa ma sono un noob in python :) could some body explain me how to indicize the several blocks ? and in particular I wrote z= N but in reality it's

 ZONE I=     41  F=POINT  TIME T=      0.0000715363

I have to catch 41 .. and read and store the follow 41 line, than after a couple of white line I encounter another ZONE section .. I don't know how many zone I there are in the file ..

Upvotes: 1

Views: 79

Answers (2)

shahaf
shahaf

Reputation: 4983

Here is a simple draft of the solution, it can be simplified, but you can take it from here

tmp_arr= list()
output=list()
with open('file.txt') as f:
  for line in f.readlines():
    if len(line.strip()) == 0:
      pass
    elif line.startswith('z='):
      counter = int(line.split('=')[-1])
    else:
      tmp_arr.append(line.split())
      counter -= 1
      if counter == 0:
        output.append(tmp_arr)
        tmp_arr = list()

output

[[['1', '2', '3'], ['2', '5', '6'], ['1', '5', '7']], [['1', '3', '9'], ['2', '5', '7']]]

Upvotes: 2

C.Nivs
C.Nivs

Reputation: 13106

I'm not sure how exactly you want to store them, but a quick generic way to do it I found with the regex module and a for loop:

import re

with open('path/to/file', 'r') as fh:
    lines = fh.readlines()

# Should give this structure
# lines = ['z=3','1 2 3', '4 5 6', '7 8 9', 'z=2', '3 5 9', '8 6 4', 'z=5', '12 3 5', '8 5 7', '1 1 1', '2 3 2', '6 6 5']

blocks = []

for i in range(len(lines)):
    if 'z=' in lines[i]:
        j = re.findall('\d',lines[i])[0]
        block = lines[i+1:i+j]
        blocks.append(block)
    else:
        continue

blocks will wind up having a list-of-lists structure: [['1 2 3', '4 5 6', ...], [...]]. You can further refine it by using the split() method, but this should get you off the ground. Granted, this does assume you can get your data into the structure at the start, which is easy if it's in a file, but I'm not sure if you are pulling from a database or not

Upvotes: 1

Related Questions