After years of working with multidimensional arrays, the Python array concept of 'List' seems quite complex to me (although it is said to be superior).
I have long (thousands of lines) 2-dimensional array files with hexadecimal values with the following format (can be with or without line separation):
[0xF26,0x70,0x66],[0x1158,0x72,0xA6],[0x1388,0x72,0xB6],
[0x15BA,0x4E,0x08],[0x17E9,0x70,0x92],[0x1A1D,0x72,0x94],
[0x1C4F,0x72,0xB4],[0x4409,0x4A,0x14], etc. etc.
I wish to use the file in Python, extracting and manipulating any random element.
I realize that I will have to convert the file into a list, and use the list.
The length of the file (number records) is dynamic, the width (elements in each record) is fixed.
Which is the most efficient Pythonian way for this?
I can change the file format if required (separation chars, etc).
New edit:
Based on some clues in the few replies I recieved, I managed to make progress, but the question is still there.
Here is what I have done, but at the end, I can not make it function like a 2-dim array, as can be seen in the attached code:
>>> test1 = open("C:/testdata1.txt", 'r') #This opens automatically as a
#list, but with line breaks and no start and end brackets.
>>> test2 = test1.read() # Convert to string
>>> test2 = test2.replace("\n","") # Remove line breaks
>>> test2 = "[" + test2 + "]" # Add brackets
>>> print(test2)
# The result looks like pure 2-dim list, but does not behave like one:
[[0x0,0x42,0x2A],[0x229,0x44,0x7C],[0x452,0x40,0x03],[0xCF9,0x4E,0x08],
[0xF26,0x70,0x66],[0x1158,0x72,0xA6],[0x1388,0x72,0xB6],]
#This gives an error
>>> print(test2[1][2])
Traceback (most recent call last):
File "<pyshell#79>", line 1, in <module>
print(test2[1][2])
IndexError: string index out of range
#But it runs like one-dim array of chars
>>> print(test2[8])
4
>>>
# If I copy and paste the above list as a new list, it works nicely!
Can better use:
>>> with open("C:/testdata1.txt", 'r') as file:
for line in file:
file.read()
# But, again, reading result with line breaks, no brackets.
'[0x229,0x44,0x7C],\n[0x452,0x40,0x03],\n[0xCF9,0x4E,0x08],
\n[0xF26,0x70,0x66],\ n[0x1158,0x72,0xA6],\n[0x1388,0x72,0xB6],'