First time here? Check out the FAQ!

Ask Your Question

Revision history [back]

click to hide/show revision 1
initial version

I think you should continue to manipulate m.str(), but using regular expressions, since m.str() already has the correct spacing. The following will work if you know that the matrix entries will be numbers (plus a possible decimal point and/or minus sign), no letters:

m = matrix([[2222,333,44444], [1,0,0]])
no_brackets =  m.str().replace('[', '').replace(']', '') # replace [, ]
add_commas = re.sub("([-.0-9]+) ", "\\1, ", no_brackets)
print(add_commas)

Output:

 2222,   333, 44444
    1,     0,     0
click to hide/show revision 2
No.2 Revision

I think you should continue to manipulate m.str(), but using regular expressions, since m.str() already has the correct spacing. The following will work if you know that the matrix entries will be numbers (plus a possible decimal point and/or minus sign), no letters:

import re
m = matrix([[2222,333,44444], [1,0,0]])
no_brackets =  m.str().replace('[', '').replace(']', '') # replace [, ]
add_commas = re.sub("([-.0-9]+) ", "\\1, ", no_brackets)
print(add_commas)

Output:

 2222,   333, 44444
    1,     0,     0