3

I have the following string:

'[[ 0.03694573  0.01084746  0.01306414  0.00499198  0.00307188]\r\n [ 0.03780498  0.02610241  0.00967986  0.02228694 -0.01153102]\r\n [ 0.03837708  0.02111896  0.01370878 -0.00136839  0.01906253]\r\n [ 0.03490672 -0.02791057  0.08824896 -0.01991765  0.01964361]\r\n [ 0.0811892   0.28361901  0.21539196 -0.00259571  0.28737829]\r\n [ 0.20022041  0.16184418  0.25939959  0.00546446  0.36415219]\r\n [ 0.22920615  0.27439767  0.36991198  0.00624375  0.39911759]]'

As you can see, it comprises of a list of lists. However, each element within the lists are separated by tabs and not commas, and therefore ast.literal_eval() does not work. Does anyone have any ideas how best to convert this into a list of lists?

BJR ​

BillyJo_rambler
  • 563
  • 6
  • 23

3 Answers3

2

Here's one way using re.sub:

import re
literal_eval(','.join(re.sub(r'(?<=\d)(\s+)(?=-?\d)', ',', s).splitlines()))

[[0.03694573, 0.01084746, 0.01306414, 0.00499198, 0.00307188],
 [0.03780498, 0.02610241, 0.00967986, 0.02228694, -0.01153102],
 [0.03837708, 0.02111896, 0.01370878, -0.00136839, 0.01906253],
 [0.03490672, -0.02791057, 0.08824896, -0.01991765, 0.01964361],
 [0.0811892, 0.28361901, 0.21539196, -0.00259571, 0.28737829],
 [0.20022041, 0.16184418, 0.25939959, 0.00546446, 0.36415219],
 [0.22920615, 0.27439767, 0.36991198, 0.00624375, 0.39911759]]
yatu
  • 86,083
  • 12
  • 84
  • 139
1

Modify the string until it can be interpreted by literal_eval. Basically this means adding a "," where needed.

If your string is called data:

import ast
data = data.replace('  ', ',').replace('\r\n ', ',')
result = ast.literal_eval(dat2)

Now result will contain your list of lists.

I'm not sure if you have spaces or tabs there. If you have tabs you'll have to replace ' ' by '\t\t'.

Matthias
  • 12,873
  • 6
  • 42
  • 48
0

This is a rough idea. Can be made smaller.

input = '[[ 0.03694573  0.01084746  0.01306414  0.00499198  0.00307188]\r\n [ 0.03780498  0.02610241  0.00967986  0.02228694 -0.01153102]\r\n [ 0.03837708  0.02111896  0.01370878 -0.00136839  0.01906253]\r\n [ 0.03490672 -0.02791057  0.08824896 -0.01991765  0.01964361]\r\n [ 0.0811892   0.28361901  0.21539196 -0.00259571  0.28737829]\r\n [ 0.20022041  0.16184418  0.25939959  0.00546446  0.36415219]\r\n [ 0.22920615  0.27439767  0.36991198  0.00624375  0.39911759]]'

input = input.replace('\r\n', ',')

array_of_arrays = input.split(',')
array_of_strings = []
for arr in array_of_arrays:
     _arr = arr.replace('[', '')
     _arr = _arr.replace(']', '')
     array_of_strings.append(_arr)
result = []
for arr in array_of_strings:
     num_strings = arr.split()
     num_arr = [float(x) for x in num_strings]
     result.append(num_arr)

print(result) 
Shahad Ishraq
  • 341
  • 2
  • 11