This has been killing me all day/night and I cannot seem to come up with a solution. Basically, I have a text file containing a 2D vector (generated from a C++ program) in doubles. I need to read this into a 2D array in Python so I can plot a spectrogram. Here is what the data looks like:
-18.2258 -18.3581 -18.7323 -19.2183 -19.8016 -20.6132 -21.8101 -22.5386 -21.8071
-20.9063 -20.4136 -20.3022 -20.3428 -20.4091 -20.6703 -21.0293 -21.5167 -22.1915
-23.0438 -23.9086 -24.5955 -26.2508 -26.0188 -22.2163 -19.933 -18.6816 -18.1048
-18.0222 18.3233 -19.0456 -20.3134 -22.7954 -25.8716 -21.4845 -19.1923 -17.9268
-17.4657 -17.3888 -16.9999 -16.4006 -15.9175 -15.8319 -16.1705 -16.6967 -17.0734
-7.92685 -10.8266 -16.392 -12.4901 -13.0831 -17.7215 -17.5159 -14.1485 -12.9897 -12.0444
-11.8363 -12.6952 -12.9652 -14.3788 -13.8465 -17.529 -17.4747 -11.9521 -12.545 -13.8976
-12.4176 -15.3273 -14.8081 -19.4117 -17.9596 -16.2607 -16.7505 -15.8918 -16.5602
-17.2225 -16.9048 -15.1381 -17.37 -16.43 -14.9437 -14.9821
Each block of data is separated by 2 lines within the text file.
I have tried the following:
with open('spec.txt') as file:
array2d = [[float(digit) for digit in line.split()] for line in file]
However, this does not work and I just seem to be getting a lot of arrays generated.
Anyone have any ideas to solve this?
P.S. each block is of the same size. However, to shorten this question, I just included a sample.
numpyarray?numpy.arrayto create a 2d array.np.loadtxt(filename)would be all you need.