Import a three-dimensional list variable from a text file in Python

I need to know if I can easily assign a variable in my script from a declaration in a text file. Basically, I want the user to be able to change the variable through a text file to match the numbers, without requiring a script.

Text file input format:

faultInfo = [
             [["L1603",1,5],[271585,972739],[272739,872739, 272739,972739, 271585,972739, 271585,272389, 270999,272389]],
             [["L1605",1,5],[271897,872739],[272739,872739, 272739,972739, 271891,872739, 271891,272119, 270963,272119]],
             [["L1607",1,4],[271584,272738],[271584,272738, 271584,272388, 270998,272388, 270998,272386]]
            ]

I just want to load this into a list variable with the same name in my script. I'm new to Python, not CS, or nothing at all. I know that I can load a three-dimensional list using loops and much more, but it seems that there should be a quick way to do this, since the size of the kth dimension is jagged and will change from case to case.

Thanks in advance.

thank

+5
source share
3

, json.loads ast.literal_eval ( ):

>>> a = """[
...              [["L1603",1,5],[271585,972739],[272739,872739, 272739,972739, 271585,972739, 271585,272389, 270999,272389]],
...              [["L1605",1,5],[271897,872739],[272739,872739, 272739,972739, 271891,872739, 271891,272119, 270963,272119]],
...              [["L1607",1,4],[271584,272738],[271584,272738, 271584,272388, 270998,272388, 270998,272386]]
...             ]
... 
... """
>>> import ast
>>> ast.literal_eval(a)
[[['L1603', 1, 5], [271585, 972739], [272739, 872739, 272739, 972739, 271585, 972739, 271585, 272389, 270999, 272389]], [['L1605', 1, 5], [271897, 872739], [272739, 872739, 272739, 972739, 271891, 872739, 271891, 272119, 270963, 272119]], [['L1607', 1, 4], [271584, 272738], [271584, 272738, 271584, 272388, 270998, 272388, 270998, 272386]]]
>>> import json
>>> json.loads(a)
[[[u'L1603', 1, 5], [271585, 972739], [272739, 872739, 272739, 972739, 271585, 972739, 271585, 272389, 270999, 272389]], [[u'L1605', 1, 5], [271897, 872739], [272739, 872739, 272739, 972739, 271891, 872739, 271891, 272119, 270963, 272119]], [[u'L1607', 1, 4], [271584, 272738], [271584, 272738, 271584, 272388, 270998, 272388, 270998, 272386]]]

(a ) file.read()

+2

.py , configuration.py amd . , faultInfo.

+2

A quick and dirty way to do this is with a function eval()if you omit the assignment. For example, if your file is param.txt:

 [
     [["L1603",1,5],[271585,972739],[272739,872739, 272739,972739, 271585,972739, 271585,272389, 270999,272389]],
     [["L1605",1,5],[271897,872739],[272739,872739, 272739,972739, 271891,872739, 271891,272119, 270963,272119]],
     [["L1607",1,4],[271584,272738],[271584,272738, 271584,272388, 270998,272388, 270998,272386]]
 ]

A simple way (as I said dirty) of loading would be the following:

with open('param.txt') as f:
    file_text = ' '.join(f.readlines()) #Join lines with a blank ' ' space
parameter = eval(file_text)
0
source

All Articles