Home > Back-end >  Is there a pain free way to import 2d array from a text file, if the file don't have any commas
Is there a pain free way to import 2d array from a text file, if the file don't have any commas

Time:10-19

I have some data from a computing cluster that is printed on to a .txt file in the form of [[a b] [c d]]. there is no commas in between each term, and adding them on manually is not realistic since it is a fairly large 2d array. I tried to use np.loadtxt(), but the data is a string according to the error message, "could not convert string to float". If anyone knows anyway to import the data as a 2d array I would greatly appreciate it.

https://www.dropbox.com/s/0uxe77z6zvnx69w/sample.txt?dl=0

Sample:

[[-5.188485 -4.172135 -3.595145 -3.37876  -3.22908  -3.17178  -3.035295
  -3.012515 -2.60205  -2.772435]
 [-5.39632  -4.377455 -3.71267  -3.22333  -3.192535 -3.125055 -2.88394
  -2.779245 -2.720865 -2.587565]
 [-5.312065 -4.102625 -3.795785 -3.257695 -3.28473  -2.96552  -2.707215
  -2.94055  -2.95986  -2.87657 ]]

CodePudding user response:

You could make the text file look like a python literal (nested lists with floats) by replacing lengths of spaces with commas. Evaluate that to a python list and use the list to create the array.

import numpy as np
import re
import ast

arr = np.array(ast.literal_eval(
        re.sub("  ", ",", open("test.txt").read())
        .replace("\n", "")))
  • Related