-
Notifications
You must be signed in to change notification settings - Fork 9
/
tprinting.py
72 lines (57 loc) · 2.2 KB
/
tprinting.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
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
#!/usr/bin/python3
# -*- coding: utf-8 -*-
# tprinting.py
#
# Copyright (C) 2015 Caian Benedicto <[email protected]>
#
# This file is part of bovespa-tools
#
# bovespa-tools is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 2, or (at your option)
# any later version.
#
# Asparagus is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
import scipy.io as sio
import numpy as np
import sys
###############################################################################
# Alguma definições
###############################################################################
tablesep = '\t'
###############################################################################
# Escreve a tabela em modo texto para um arquivo
###############################################################################
def writeTable(file, header, table):
if file == '':
file = sys.stdout
closefile = False
else:
file = open(file, 'wt')
closefile = True
colszs = [len(title) for title in header]
for i in range(0, len(colszs)):
colszs[i] = max([colszs[i]]+[len(row[i]) for row in table])
file.write(
tablesep.join([i[1].ljust(i[0]) for i in zip(colszs, header)]) + '\n'
)
for row in table:
file.write(
tablesep.join([i[1].ljust(i[0]) for i in zip(colszs, row)]) + '\n'
)
###############################################################################
# Salva a tabela como arquivo .mat do matlab
###############################################################################
def writeMat(filename, header, table):
# Cria uma matriz de células para guardar os elementos
a = np.empty((len(table),len(header)),dtype=object)
# Salva a tabela nas células
for i in range(len(table)):
for j in range(len(header)):
a[i,j] = table[i][j]
# Escreve a tabela
sio.savemat(filename, {'header' : header, 'data' : a})