-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathread_blocks.py
56 lines (48 loc) · 1.75 KB
/
read_blocks.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
import numpy as np
import pandas as pd
import types
def read_blocks(f, i=0, j=-1, columns=None):
"""
http://stackoverflow.com/questions/10512026/reading-data-blocks-from-a-file-in-python
"""
# # def myfunc(a,b, *args, **kwargs):
# i = kwargs.get('i', 0)
# j = kwargs.get('j', 1)
if (type(f) == types.StringType or type(f) == types.UnicodeType ):
fo = open(f, 'r')
matrix = read_blocks(fo,i,j,columns=columns)
fo.close()
return matrix
elif type(f) == types.FileType:
# print i,j
new_block = False
blocks = [[]]
blockcount = 0
for line in f:
line = line.replace('\r\n', '')
line = line.replace('\n', '')
line = line.replace('\r', '')
# Check for empty/commented lines
if not line or line.startswith('#'):
new_block = True
if blockcount == j:
break
blockcount += 1
# Non empty line: add line in current(last) block
else:
# If 1st one: new block
if new_block == True and blocks[-1] != []:
blocks.append([])
new_block = False
# print line.split()
newline = map(float,line.split())
# print(np.shape(newline))
blocks[-1].append(np.array(newline))
blocks = np.array(blocks)
if blocks[-1] == []:
np.delete(block,-1)
frames = []
for i in blocks:
frames.append(pd.DataFrame(i,columns=columns))
return pd.concat(frames)
raise TypeError('f must be a file object or a file name.')