3
$\begingroup$

Consider numeric data that has the form

{{{a,{b,c,d},e,f,g,h,k,{l,m,n},o}, ...},...},

where the first ... mean other similar rows, while the second ... mean similar structures. A toy example is

data={{{535, {-10.2125, -20.2039, 35.0112}, 42.1021, 7.21051, 0.573983, 
  1.22031, -2.03881, {0., 0., 0.}, 
  0}, {521, {-9.02828, -17.8807, 31.429}, 37.6437, 7.13084, 0.567418, 
  1.23246, -2.03837, {0., 0., 0.}, 
  0}, {-321, {-1.18421, -2.32325, 3.58218}, 4.45847, 9.03114, 
  0.629239, 1.12261, -2.04219, {0., 0., 0.}, 
  0}, {-421, {-3.03676, -3.47709, 6.20393}, 7.95548, 4.26567, 0.63973,
   1.10491, -2.2887, {-0.000127805, -0.000253119, 0.00044491}, 
  0.000527587}, {-11, {-0.622267, -2.0742, 3.33932}, 3.98024, 7789.12,
   0.575316, 
  1.21786, -1.86226, {-0.000127805, -0.000253119, 0.00044491}, 
  0.000527587}, {18000, {-5.36925, -12.3294, 21.8857}, 25.708, 25.708,
   0.550979, 
  1.26345, -1.98151, {-0.000127805, -0.000253119, 0.00044491}, 
  0.000527587}, {-211, {-1.3488, -1.40502, 1.29289}, 2.34223, 16.7818,
   0.984764, 
  0.622779, -2.33578, {-0.000331106, -0.000485899, 0.000860242}, 
  0.00104199}, {321, {-1.68796, -2.07207, 4.91103}, 5.61325, 11.3703, 
  0.498379, 
  1.36854, -2.25439, {-0.000331106, -0.000485899, 0.000860242}, 
  0.00104199}},{{535, {-3.08825, 1.62629, 60.6735}, 61.0522, 10.4559, 0.0574624, 
  3.5495, 2.65689, {0., 0., 0.}, 
  0}, {521, {-2.67021, 1.28728, 55.4199}, 55.7482, 10.5604, 0.0534371,
   3.62216, 2.69238, {0., 0., 0.}, 
  0}, {-321, {-0.418043, 0.339014, 5.25358}, 5.30396, 10.7438, 
  0.102094, 2.97414, 2.46021, {0., 0., 0.}, 
  0}, {-421, {-2.75151, 0.00555734, 28.1596}, 28.3544, 15.2034, 
  0.0974022, 3.02126, 
  3.13957, {-0.000223351, 0.000107675, 0.00463563}, 
  0.00464226}, {-11, {-0.786279, 0.0256629, 5.24043}, 5.29901, 
  10369.9, 0.149008, 2.59505, 
  3.10897, {-0.000223351, 0.000107675, 0.00463563}, 
  0.00464226}, {18000, {0.86758, 1.25606, 22.0199}, 22.0948, 22.0948, 
  0.0692156, 3.36328, 
  0.966329, {-0.000223351, 0.000107675, 0.00463563}, 
  0.00464226}, {-211, {-1.68904, 0.832884, 15.597}, 15.7106, 112.564, 
  0.120161, 2.81087, 2.68347, {-0.000238726, 0.000107706, 0.00479298},
   0.00480013}, {321, {-1.06247, -0.827326, 12.5626}, 12.6438, 
  25.6116, 0.106783, 
  2.92915, -2.47999, {-0.000238726, 0.000107706, 0.00479298}, 
  0.00480013}}}

I export it using the following command:

Export["dat.dat", data, "Table"]

When importing, the structure becomes broken:

dataImp=Import["dat.dat", "Table"]

{{"{535,", "{-10.2125,", "-20.2039,", "35.0112},", "42.1021,",
"7.21051,", "0.573983,", "1.22031,", "-2.03881,", "{0.,", "0.,",
"0.},", "0}", "{521,", "{-9.02828,", "-17.8807,", "31.429},",
"37.6437,", "7.13084,", "0.567418,", "1.23246,", "-2.03837,",
"{0.,", "0.,", "0.},", "0}", "{-321,", "{-1.18421,", "-2.32325,",
"3.58218},", "4.45847,", "9.03114,", "0.629239,", "1.12261,",
"-2.04219,", "{0.,", "0.,", "0.},", "0}", "{-421,", "{-3.03676,",
"-3.47709,", "6.20393},", "7.95548,", "4.26567,", "0.63973,",
"1.10491,", "-2.2887,", "{-0.000127805,", "-0.000253119,",
"0.00044491},", "0.000527587}", "{-11,", "{-0.622267,", "-2.0742,",
"3.33932},", "3.98024,", "7789.12,", "0.575316,", "1.21786,",
"-1.86226,", "{-0.000127805,", "-0.000253119,", "0.00044491},",
"0.000527587}", "{18000,", "{-5.36925,", "-12.3294,", "21.8857},",
"25.708,", "25.708,", "0.550979,", "1.26345,", "-1.98151,",
"{-0.000127805,", "-0.000253119,", "0.00044491},", "0.000527587}",
"{-211,", "{-1.3488,", "-1.40502,", "1.29289},", "2.34223,",
"16.7818,", "0.984764,", "0.622779,", "-2.33578,", "{-0.000331106,",
"-0.000485899,", "0.000860242},", "0.00104199}", "{321,",
"{-1.68796,", "-2.07207,", "4.91103},", "5.61325,", "11.3703,",
"0.498379,", "1.36854,", "-2.25439,", "{-0.000331106,",
"-0.000485899,", "0.000860242},", "0.00104199}"}, {"{535,",
"{-3.08825,", "1.62629,", "60.6735},", "61.0522,", "10.4559,",
"0.0574624,", "3.5495,", "2.65689,", "{0.,", "0.,", "0.},", "0}",
"{521,", "{-2.67021,", "1.28728,", "55.4199},", "55.7482,",
"10.5604,", "0.0534371,", "3.62216,", "2.69238,", "{0.,", "0.,",
"0.},", "0}", "{-321,", "{-0.418043,", "0.339014,", "5.25358},",
"5.30396,", "10.7438,", "0.102094,", "2.97414,", "2.46021,", "{0.,",
"0.,", "0.},", "0}", "{-421,", "{-2.75151,", "0.00555734,",
"28.1596},", "28.3544,", "15.2034,", "0.0974022,", "3.02126,",
"3.13957,", "{-0.000223351,", "0.000107675,", "0.00463563},",
"0.00464226}", "{-11,", "{-0.786279,", "0.0256629,", "5.24043},",
"5.29901,", "10369.9,", "0.149008,", "2.59505,", "3.10897,",
"{-0.000223351,", "0.000107675,", "0.00463563},", "0.00464226}",
"{18000,", "{0.86758,", "1.25606,", "22.0199},", "22.0948,",
"22.0948,", "0.0692156,", "3.36328,", "0.966329,", "{-0.000223351,",
"0.000107675,", "0.00463563},", "0.00464226}", "{-211,",
"{-1.68904,", "0.832884,", "15.597},", "15.7106,", "112.564,",
"0.120161,", "2.81087,", "2.68347,", "{-0.000238726,",
"0.000107706,", "0.00479298},", "0.00480013}", "{321,",
"{-1.06247,", "-0.827326,", "12.5626},", "12.6438,", "25.6116,",
"0.106783,", "2.92915,", "-2.47999,", "{-0.000238726,",
"0.000107706,", "0.00479298},", "0.00480013}"}}

dataImp[[1]][[1]]

"{535,"

Could you please tell me how to fix exporting/importing such that the structure of the initial data would be untouched?

Edit. It turns out that exporting/importing in format "MX" does work.

$\endgroup$
1
  • $\begingroup$ Checking the $ExportFormats: the ".dat" is not a supported format. Perhaps reorganizing you data into a List of Associations is more practical. $\endgroup$ Commented Nov 27, 2021 at 15:16

1 Answer 1

2
$\begingroup$

Don't use "Table" in Export and Import functions, i.e., if you use

Export[NotebookDirectory[] <> "teste.txt", data]

You will get the same data when importing with

{Import[NotebookDirectory[] <> "teste.txt"]}

That is:

{{{535, {-10.2125, -20.2039, 35.0112}, 42.1021, 7.21051, 0.573983, 1.22031, -2.03881, {0., 0., 0.}, 0}, {521, {-9.02828, -17.8807, 31.429}, 37.6437, 7.13084, 0.567418, 1.23246, -2.03837, {0., 0., 0.}, 0}, {-321, {-1.18421, -2.32325, 3.58218}, 4.45847, 9.03114, 0.629239, 1.12261, -2.04219, {0., 0., 0.}, 0}, {-421, {-3.03676, -3.47709, 6.20393}, 7.95548, 4.26567, 0.63973, 1.10491, -2.2887, {-0.000127805, -0.000253119, 0.00044491}, 0.000527587}, {-11, {-0.622267, -2.0742, 3.33932}, 3.98024, 7789.12, 0.575316, 1.21786, -1.86226, {-0.000127805, -0.000253119, 0.00044491}, 0.000527587}, {18000, {-5.36925, -12.3294, 21.8857}, 25.708, 25.708, 0.550979, 1.26345, -1.98151, {-0.000127805, -0.000253119, 0.00044491}, 0.000527587}, {-211, {-1.3488, -1.40502, 1.29289}, 2.34223, 16.7818, 0.984764, 0.622779, -2.33578, {-0.000331106, -0.000485899, 0.000860242}, 0.00104199}, {321, {-1.68796, -2.07207, 4.91103}, 5.61325, 11.3703, 0.498379, 1.36854, -2.25439, {-0.000331106, -0.000485899, 0.000860242}, 0.00104199}}\n{{535, {-3.08825, 1.62629, 60.6735}, 61.0522, 10.4559, 0.0574624, 3.5495, 2.65689, {0., 0., 0.}, 0}, {521, {-2.67021, 1.28728, 55.4199}, 55.7482, 10.5604, 0.0534371, 3.62216, 2.69238, {0., 0., 0.}, 0}, {-321, {-0.418043, 0.339014, 5.25358}, 5.30396, 10.7438, 0.102094, 2.97414, 2.46021, {0., 0., 0.}, 0}, {-421, {-2.75151, 0.00555734, 28.1596}, 28.3544, 15.2034, 0.0974022, 3.02126, 3.13957, {-0.000223351, 0.000107675, 0.00463563}, 0.00464226}, {-11, {-0.786279, 0.0256629, 5.24043}, 5.29901, 10369.9, 0.149008, 2.59505, 3.10897, {-0.000223351, 0.000107675, 0.00463563}, 0.00464226}, {18000, {0.86758, 1.25606, 22.0199}, 22.0948, 22.0948, 0.0692156, 3.36328, 0.966329, {-0.000223351, 0.000107675, 0.00463563}, 0.00464226}, {-211, {-1.68904, 0.832884, 15.597}, 15.7106, 112.564, 0.120161, 2.81087, 2.68347, {-0.000238726, 0.000107706, 0.00479298}, 0.00480013}, {321, {-1.06247, -0.827326, 12.5626}, 12.6438, 25.6116, 0.106783, 2.92915, -2.47999, {-0.000238726, 0.000107706, 0.00479298}, 0.00480013}}}

$\endgroup$
1
  • $\begingroup$ The method does not work, especially in the case of many blocks {}. $\endgroup$ Commented Nov 26, 2021 at 23:38

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.