1

In Python, I would like to store numpy arrays, matrices and possibly later other objects in plain text format.

My idea was to use ConfigParser and define parser array2string, matrix2string, string2array and string2matrix (there is numpy.array2string and matrix2string could be implemented based on that, but I couldn't find functions for the reverse). Then writing looks like:

config.set('calibration', 'center', array2string(center))
config.set('calibration', 'trans_matrix', matrix2string(trans_matrix))

and reading like:

center = string2array(config.get('calibration', 'center'))
trans_matrix = string2matrix(config.get('calibration', 'trans_matrix'))

What is the best way to write and read the numpy objects?

5
  • In Python 3.4, Numpy 1.9, I am able to find array2string, but I cannot find string2array, matrix2string and string2matrix. From which package are these functions? Commented Jun 7, 2015 at 9:31
  • 2
    There are loads of methods to save arrays / matrices as strings in numpy. Start with array_repr, numpy.savetxt Commented Jun 7, 2015 at 9:32
  • array2string, matrix2string, string2array and string2matrix are placeholders for the functions to be implemented. numpy.savetxt and numpy.loadtxt write to files and not strings and can't be used to write multiple arrays to a file. I couldn't find reverse functions for numpy.array2string and the like. Commented Jun 7, 2015 at 9:45
  • 1
    @magmabyte You could just use savetxt to save an array into a temporary file, read it as a string and remove the file after. Seriously, numpy provides a lot of useful methods. Have you tried them? Also there is pickle in standard Python library. Commented Jun 7, 2015 at 10:01
  • Writing to file and reading it in back as string to save it to another file for saving and vice versa for reading doesn't sound like an elegant solution. I am using pickle right now but it does not save the objects in plain text. I thought that there maybe is an elegant solution to it (especially one that writes and reads from strings and therefore allows me to use ConfigParser). I guess I'll use numpy.array2string for writing arrays and matrices and write the string2 parsers myself. Commented Jun 7, 2015 at 11:55

1 Answer 1

0

Answer in this post gives a nice function which works well:

how to read numpy 2D array from string?

import configparser
import re
import ast
import numpy as np


def str2array(s):
    # Remove space after [
    s=re.sub('\[ +', '[', s.strip())
    # Replace commas and spaces
    s=re.sub('[,\s]+', ', ', s)
    return np.array(ast.literal_eval(s))


config = configparser.ConfigParser()
config.read("config.ini")
var_a = config.get("myvars", "var_a")
var_b = config.get("myvars", "var_b")
var_c = config.get("myvars", "var_c")


var_d=config.get("myvars", "var_d")

var_e=config.get("myvars", "var_e")

q=str2array(var_d)
r=str2array(var_e)

Where the config.ini file is:

# Variable tests
#
#

[myvars]
var_a='home'
var_b='car'
var_c=15.5
var_d=[2.1 3.4 4.6]
var_e=[[  1.1   2.2  2.233]
 [  1.00000000e+02   1.17809494e+03   4.9410e+02]
 [  2.00000000e+04   1.20e+01   1.2323e+04]]
Sign up to request clarification or add additional context in comments.

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.