3

I have a txt (string) file looking like:

[[[[0.17262284, 0.95086717, 0.01172171, 0.79262904],
     [0.52454078, 0.16740103, 0.32694925, 0.78921072],
     [0.77886716, 0.35550488, 0.89272706, 0.36761104]],
    [[0.14336841, 0.94488079, 0.83388505, 0.02065268],
     [0.31804594, 0.22056339, 0.84088501, 0.94994676],
     [0.57845057, 0.12645735, 0.12628646, 0.05526736]]]]

The shape is (1,2,3,4). How can I easily convert it to a NumPy array that considers the data type and the shape (that may be deduced from the parenthesis)? Let's say I have the shape in the txt file, will it make it easier? I have tried using np.loadtxt, seems that it doesn't handle this 4d shape, it loads as a 1D array. I can parse the shape myself and then convert all members to floats but I believe there is np function that already does that. The file is not created by me - I would just pkl the array.

1
  • How did you create the file? What exactly happened when you "tried using np.loadtxt"? Commented Nov 24, 2020 at 12:38

2 Answers 2

4

The text string containing the array can be evaluated ('read') using the ast.literal_eval() function, then wrapped in a numpy array constructor, as:

import ast
import numpy as np


# Option 1: From a text string.
a = np.array(ast.literal_eval(txt))

# Option 2: Directly from text file.
a = np.array(ast.literal_eval(open('np_array.txt').read()))

a.shape
>>> (1, 2, 3, 4)

Setup:

txt = """[[[[0.17262284, 0.95086717, 0.01172171, 0.79262904],
[0.52454078, 0.16740103, 0.32694925, 0.78921072],
[0.77886716, 0.35550488, 0.89272706, 0.36761104]],
[[0.14336841, 0.94488079, 0.83388505, 0.02065268],
[0.31804594, 0.22056339, 0.84088501, 0.94994676],
[0.57845057, 0.12645735, 0.12628646, 0.05526736]]]]"""
Sign up to request clarification or add additional context in comments.

2 Comments

Huh. A documented, built-in, safe version of eval. Haven't seen that before.
@DanielF - Yes, a very useful / safe tool. :-)
3

This array encoding matches the encoding scheme of json, so you can use that to load it:

import json
import numpy as np

arr = np.array(json.load(open("array.txt", "r")))

Or if the data is a string, use:

arr = np.array(json.loads(array_as_string))

Comments

Your Answer

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.