0

I'm using python 3.7.

I have several sets of data in the text file dict.txt:

A = {(1, 1): 11, (2, 2): 22, (3, 3): 33, (7, 7): 77, (8, 8): 88, (9, 9): 99}
B = [[ 0  2]
 [ 0  4]
 [ 0  5]
 [ 0  6]]
C = 0.2
D = [1 2 3 4 5]

I want to input Line1 A = {(1, 1): 11, (2, 2): 22, (3, 3): 33, (7, 7): 77, (8, 8): 88, (9, 9): 99} as a dict.

And I want to input B as a ndarray.

But I don't want to input the data through the method of indexing by line number.

Is there any way I can input the data through the method of indexing by the keyword, like A, B, C, or D?

Drizzle
  • 95
  • 1
  • 6
  • My guess is that this is only possible if you preprocess the file or search the file for your desired keywords and then parse out the code text and pass it into `eval` or `exec`. AFAIK, files can only be accessed sequentially (i.e. character by character or line by line) and not by keyword, but someone here may know better. – joseville Nov 07 '21 at 14:10
  • 1
    Does this answer your question? [Convert a String representation of a Dictionary to a dictionary?](https://stackoverflow.com/questions/988228/convert-a-string-representation-of-a-dictionary-to-a-dictionary) – Tomerikoo Nov 07 '21 at 14:11

3 Answers3

1

Instead of using a text file, you could use other serialization techniques like pickle.

A text file is not the right approach to store this and then restore it, without having to do unnecessary parsing of various data structures, especially NumPy array that are not part of standard lib and can't be reversed using say ast.literal_eval.

It's much easier to just dump them into a pickle file and load it back(unless there is some importance to dumping them into a text file for a human to read):

>>> A = {(1, 1): 11, (2, 2): 22, (3, 3): 33, (7, 7): 77, (8, 8): 88, (9, 9): 99}
>>> B = numpy.array([[0, 2], [4, 6], [0, 5], [0, 6]])
>>> C = 0.2
>>> D = numpy.array([1, 2, 3, 4, 5])


>>> import pickle
>>> with open("foo.pkl", "wb") as f:
...     pickle.dump({"A": A, "B": B, "C": C, "D": D}, f)
...
>>> with open("foo.pkl", "rb") as f:
...     data = pickle.load(f)
...
>>> data
{'A': {(1, 1): 11, (2, 2): 22, (3, 3): 33, (7, 7): 77, (8, 8): 88, (9, 9): 99}, 'B': array([[0, 2],
       [4, 6],
       [0, 5],
       [0, 6]]), 'C': 0.2, 'D': array([1, 2, 3, 4, 5])}
>>> data["B"]
array([[0, 2],
       [4, 6],
       [0, 5],
       [0, 6]])
Ashwini Chaudhary
  • 244,495
  • 58
  • 464
  • 504
0

To answer your second question re:

I want to input Line1 A = {(1, 1): 11, (2, 2): 22, (3, 3): 33, (7, 7): 77, (8, 8): 88, (9, 9): 99} as a dict.

Here are two ways to do it:

exec("A = {(1, 1): 11, (2, 2): 22, (3, 3): 33, (7, 7): 77, (8, 8): 88, (9, 9): 99}")

or

A = eval("{(1, 1): 11, (2, 2): 22, (3, 3): 33, (7, 7): 77, (8, 8): 88, (9, 9): 99}")

However, this opens you up to executing possibly dangerous code if you are not the one who controls dict.txt.

joseville
  • 685
  • 3
  • 16
0

If it’s well-formatted, you could simply use the import statement; that will save you from having to manipulate to create well-formed statements/declarations.

Given a file mydecs.py whose contents are:

A = {(1, 1): 11, (2, 2): 22, (3, 3): 33, (7, 7): 77, (8, 8): 88, (9,9): 99}
B = np.array([[0,2],[0,4],[0,5], [0,6]]) 
C = 0.2 
D = [1, 2, 3, 4, 5]

Importing mydecs module makes the 'data' available simply by typing their names, as shown below:

from mydecs import *
>>> A
{(1, 1): 11, (2, 2): 22, (3, 3): 33, (7, 7): 77, (8, 8): 88, (9, 9): 99}
>>> 
>>> B 
array([[0, 2],
       [0, 4],
       [0, 5],
       [0, 6]])
>>> C
0.2
>>> D
[1, 2, 3, 4, 5]
Jeremy Caney
  • 7,102
  • 69
  • 48
  • 77
  • As it’s currently written, your answer is unclear. Please [edit] to add additional details that will help others understand how this addresses the question asked. You can find more information on how to write good answers [in the help center](/help/how-to-answer). – jahantaila Nov 07 '21 at 16:21