# How to combine two large tables and export them to .txt file?

I have two large tables. I shall post only 2 rows out of 100+. I shall name the first table blabla and the elements are:

blabla = {{1., 0.0118897, 0.0218102, 0.0166538, 0.026621,
0.0195213, 0.0295166, 0.0171602, 0.0271323, 0.0218905,
0.0319091, 0.0194651, 0.0294599, 0.0166805, 0.026648,
0.0204106, 0.0304147, 0.0228323, 0.0328601, 0.021254,
0.0312663, 0.0233098, 0.0333423, 0.021873, 0.0318914,
0.0174677, 0.0274429, 0.0188054, 0.0287937, 0.0219893,
0.0320088, 0.0309231, 0.0410302, 0.0285016, 0.0385849,
0.0296551, 0.0397497, 0.0350313, 0.0451787, 0.0304206,
0.0405227, 0.0428235, 0.0530473, 0.0432169, 0.0534446,
0.03005, 0.0401486, 0.0286906, 0.0387758, 0.0304793,
0.040582, 0.0804419, 0.0910345, 0.142272, 0.153471,
0.160546, 0.171924}, {2., 0.0150861, 0.025038, 0.0196577,
0.0296543, 0.0214633, 0.0314777, 0.0205533, 0.0305587,
0.0239663, 0.0340052, 0.0222613, 0.0322834, 0.0208354,
0.0308436, 0.0234556, 0.0334895, 0.0244662, 0.0345099,
0.0252611, 0.0353126, 0.0252723, 0.035324, 0.0250053,
0.0350544, 0.0220828, 0.0321032, 0.0221404, 0.0321613,
0.0256944, 0.0357503, 0.0317988, 0.0419144, 0.0336531,
0.043787, 0.03152, 0.0416329, 0.0356098, 0.0457629,
0.0360809, 0.0462385, 0.0444764, 0.0547163, 0.0501084,
0.0604036, 0.0329392, 0.043066, 0.0364718, 0.0466333,
0.0359922, 0.046149, 0.0869363, 0.0975925, 0.143037,
0.154243, 0.199699, 0.21146}};


while the second table is

neki = {{0.0202148, 0.0274192, 0.0207453, 0.0262675, 0.0189993,
0.0233971, 0.022678, 0.0285582, 0.0209791, 0.0253666,
0.0213924, 0.0263567, 0.0260517, 0.0329767, 0.0223663,
0.0273434, 0.0196478, 0.0236058, 0.0259541, 0.0315261,
0.0201568, 0.0241432, 0.0219558, 0.0265508, 0.0258081,
0.0323964, 0.019427, 0.0240745, 0.0214446, 0.0259115,
0.0193406, 0.0223116, 0.0274793, 0.0320205, 0.0185941,
0.0215596, 0.0186161, 0.0211728, 0.0260151, 0.0300705,
0.0218959, 0.0244066, 0.0310664, 0.0345995, 0.0150838,
0.0174611, 0.0252581, 0.0294076, 0.0162781, 0.0188112,
0.0319529, 0.034044, 0.0404893, 0.0421196, 0.040583,
0.0420639}, {0.0227813, 0.0293923, 0.0225488, 0.0277362,
0.0199277, 0.0241689, 0.0248315, 0.0303233, 0.0219581,
0.0261947, 0.0228869, 0.0276024, 0.029134, 0.0355,
0.0239875, 0.0287053, 0.0203436, 0.0241971, 0.028312,
0.0335241, 0.0209943, 0.0248577, 0.0234862, 0.0278493,
0.0290378, 0.0350636, 0.0210898, 0.0254561, 0.0231937,
0.0273992, 0.0196151, 0.0225536, 0.0298824, 0.034137,
0.0191751, 0.0220705, 0.0187708, 0.0213111, 0.028356,
0.0321484, 0.0223199, 0.0247937, 0.0334858, 0.0368208,
0.015799, 0.0180922, 0.0285108, 0.0322872, 0.0177035,
0.0200765, 0.0332491, 0.0352824, 0.0406024, 0.0422301,
0.0455114, 0.0469092}};


What I want it so add the columns of neki table at the of blabla table. I tried to combine them with the following code

FinalOutput = Join[blabla, neki, 2];


Now here is the catch I don't understand. If I export that FinalOutput like that:

Export["results_output.txt",
StringReplace[ToString[TableForm[FinalOutput]], "\n\n" -> "\n"]];


I get some weird spacing. The columns are not aligned as they should be. I attached the result to this code in the dropbox map. Yet if I type finalOutput//TableForm in mathematica the output is perfect - just the way I want it to be. So I assume something is wrong with my export code, but what?

• It would be much easier to help if you were to provide a small data set directly in the question. The code you show cannot be evaluated due to that, and downloading data and code from other locations can be awkward. Your export line seems somewhat convoluted. – Yves Klett Dec 20 '15 at 14:01
• @YvesKlett Good point, I have edited the OP. – skrat Dec 20 '15 at 14:16
• It appears to be a whitespace problem. When you convert 1 to a string as opposed to 0.1234 to a string, both have different string lengths. I would convert the individual entries to strings and add whitespace so that everyone is the same length. – Wintermute Dec 20 '15 at 15:11
• @Wintermute I'm not really sure how to do that. – skrat Dec 20 '15 at 16:16

Here is the implementation of Wintermute's idea (see comments in OP question) :

stringTable = Map[ToString, finalOutput, {2}];
maxStringLength = Max[Flatten[Map[StringLength, stringTable, {2}]]];
finalOutput300 =
Map[RightComposition[Characters,
PadRight[#, maxStringLength, " "] &, StringJoin],
stringTable, {2}];
Export["results_output300.txt",
StringReplace[
ToString[TableForm[finalOutput300, TableSpacing -> {1, 1}]],
"\n\n" -> "\n"]];


Verification :

fileContent = ReadList["results_output300.txt", String];
header = fileContent[[1]] // StringLength //
Table[Mod[i, 10], {i, 1, #}] & // (ToString /@ # &) // StringJoin;