In [1]:
import os
import warnings
import numpy as np
import pandas
import csv
import re

In [2]:
def get_type(data):
    return type(data)

In [3]:
a = np.array([[1,2,3], [3, 5, 6]])

In [5]:
a_string = np.array2string(a)
a_string


Out[5]:
'[[1 2 3]\n [3 5 6]]'

In [7]:
a_string[2:][:-2]


Out[7]:
'1 2 3]\n [3 5 6'

In [13]:
regex_1 = r"\[|\]"
regex_2 = r"\n "
regex_3 = r" "

repl_1 = r""
repl_2 = r"\\"
repl_3 = r","

for regex_repl in zip((regex_1, regex_2, regex_3), (repl_1, repl_2, repl_3)):
    a_string = re.sub(regex_repl[0], regex_repl[1], a_string)

In [12]:
re.sub(regex_3, r"&", a_string[2:][:-2])


Out[12]:
'1&2&3]\n&[3&5&6'

In [173]:



Out[173]:
'[[     1      3    505     42    434]\n [     2     43     43     43  -4343]\n [   432     42     43     42 424042]]'

In [215]:
# Getting the largest number
a = np.array([[1, 3, 505, 42, 434], [2, 43, 43, 43, -4343], [432, 42, 43, 42, 424042]])
string_array = np.array2string(a)

a_string = a_string[2:][:-2]
regex_1 = r"\[|\]"
regex_2 = "\n "
regex_3 = r" "
repl_1 = r""
repl_2 = r"\\\\\n"
repl_3 = " & "
for regex_repl in zip((regex_1, regex_2), (repl_1, repl_2)):
    string_array = re.sub(regex_repl[0], regex_repl[1], string_array)
# We need to do second pass. I really wish if there is an easy way of doing that
array_removed_spaces = " ".join(string_array.split())
string_array = re.sub(regex_3, repl_3, array_removed_spaces)
# Adding the extra \n
print("\\\\\n".join(string_array.split(r"\\")))


1 & 3 & 505 & 42 & 434\\
 & 2 & 43 & 43 & 43 & -4343\\
 & 432 & 42 & 43 & 42 & 424042

In [166]:



Out[166]:
'     &     &   50&    4&   434\\\\\n     &    4&    4&    4& -4343\\\\\n   43&    4&    4&    4&424042'

In [50]:
5*"s"


Out[50]:
'sssss'

In [331]:
def tolatex(array, header, centring=True, caption="My table", label="table_something"):
    """
    This function converts numpy ndarray into a valid latex table.
    """
    string_array = np.array2string(array)[2:][:-2] # I use this for tables items
    regex_1 = r"\[|\]"
    regex_2 = "\n "
    regex_3 = r" "
    repl_1 = r""
    repl_2 = r"\\\\\n"
    repl_3 = " & "
    
    a_string = re.sub(regex_1, repl_1, string_array) # I use this to compute the largest number length
    string_table_items = a_string.replace("\n", "").split(" ")
    length_table_items = max(len(str(i)) for i in string_table_items)
    
    template = """\\begin{table}[]
      %s
      \\caption{%s}
      \\label{%s}
      \\begin{tabular}{@{%s}@{}}
      \\toprule
    """ % (centring, caption, label, len(header) * "l")
    length_header_items = max(len(i) for i in header)
    padding = " " * (max(length_table_items, length_header_items) // 2) + "&" + " " * (max(length_table_items, length_header_items) // 2)
    midrule = "  " + padding.join(header) + r"\\" + "\n" + " "* (max(length_table_items, length_header_items) // 2 + 2) + "\midrule" + "\n"
    template += midrule
    columns, rows = array.shape
    if columns != header:
        warnings.warn("The length of header doesn't match with the length of \
        the column. We will placeholder it with an empty string.")
    for regex_repl in zip((regex_1, regex_2), (repl_1, repl_2)):
        string_array = re.sub(regex_repl[0], regex_repl[1], string_array)
    # We need to do second pass. I really wish if there is an easy way of doing that
    array_removed_spaces = " ".join(string_array.strip().split())
    string_array = re.sub(regex_3, repl_3, array_removed_spaces)
    # Adding the extra \n
    string_array = "\t\\\\\n\t".join(string_array.split(r"\\"))
    string_array = "\t" + string_array + "\\\\\n"+ "    \\bottomrule" + "\n" + "    \\end{tabular}" + "\n" + "\\end{table}"
    
    return template + string_array

In [332]:
a = np.random.rand(7, 2)
print(tolatex(a, header=["One", "Two", "Three", "Four", "Five"]))


\begin{table}[]
      True
      \caption{My table}
      \label{table_something}
      \begin{tabular}{@{lllll}@{}}
      \toprule
      One     &     Two     &     Three     &     Four     &     Five\\
       \midrule
	0.16631292 & 0.97702809	\\
	 & 0.20791699 & 0.67693387	\\
	 & 0.51874648 & 0.57966175	\\
	 & 0.77753471 & 0.4093189 & 	\\
	 & 0.51863438 & 0.88115219	\\
	 & 0.60895347 & 0.01832751	\\
	 & 0.80437334 & 0.62710777\\
    \bottomrule
    \end{tabular}
\end{table}
/home/adonese/anaconda3/lib/python3.5/site-packages/ipykernel/__main__.py:30: UserWarning: The length of header doesn't match with the length of         the column. We will placeholder it with an empty string.

In [375]:
array = np.random.rand(6, 2)
string_array = np.array2string(array)[2:][:-2] # I use this for tables items
regex_1 = r"\[|\]"
regex_2 = "\n "
regex_3 = r" "
repl_1 = r""
repl_2 = r"\\\\\n"
repl_3 = " & "

a_string = re.sub(regex_1, repl_1, string_array) # I use this to compute the largest number length
string_table_items = a_string.replace("\n", "").split(" ")
length_table_items = max(len(str(i)) for i in string_table_items)

template = """\\begin{table}[]
  %s
  \\caption{%s}
  \\label{%s}
  \\begin{tabular}{@{%s}@{}}
  \\toprule
""" % (centring, caption, label, len(header) * "l")
length_header_items = max(len(i) for i in header)
padding = " " * (max(length_table_items, length_header_items) // 2) + "&" + " " * (max(length_table_items, length_header_items) // 2)
midrule = "  " + padding.join(header) + r"\\" + "\n" + " "* (max(length_table_items, length_header_items) // 2 + 2) + "\midrule" + "\n"
template += midrule
columns, rows = array.shape
if columns != header:
    warnings.warn("The length of header doesn't match with the length of \
    the column. We will placeholder it with an empty string.")
for regex_repl in zip((regex_1, regex_2), (repl_1, repl_2)):
    string_array = re.sub(regex_repl[0], regex_repl[1], string_array)
# We need to do second pass. I really wish if there is an easy way of doing that
array_removed_spaces = " ".join(string_array.split())

string_array = re.sub(regex_3, repl_3, array_removed_spaces)
string_array = string_array.split("\\\\")


['0.06021033 & 0.33597086', '0.507968750.50427236', '0.645672580.16201692', '0.840258750.43529734', '0.519282170.8519447', '0.975711230.55873163']
/home/adonese/anaconda3/lib/python3.5/site-packages/ipykernel/__main__.py:27: UserWarning: The length of header doesn't match with the length of     the column. We will placeholder it with an empty string.

In [362]:
a.shape


Out[362]:
(7, 2)

In [ ]: