Solution for Difficulty importing and OUT file into python with square brackets
is Given Below:
So this should be a super simple/easy fix out there but I have been unable to find it due to being an idiot.
I have a set of data in the format as an “.out” from another python code.
The data looks like
[0.050626139320072316, 0.4897246945768088, 0.279325751665853, 0.010591097495509572] [0.012328732333543344, 0.14501277198780135, 0.16484825763980457, 0.0016252082721251253]
and goes on for 100s of more lines. I feel that this is a standard output, but apparently none of the basic methods of importing like it. I’m trying to import this data to work on, without changing the “.out” file itself.
frequencies = open("filelocation") frequencies = pd.read_csv("filelocation") frequencies = np.genfromtxt("filelocation",Dtype=['<8f','<8f','<8f','<8f']
The data has to be usable for creating graphs in the code. You may ask yourselves what the issue with this is? Well for whatever reason all of these methods of importing the data thinks that the first and last number in a line is a string with the square bracket attached to the number. And any method to eliminate the square bracket I have so far made has destroyed the structure of the array so that it is only a list.
As mentioned in the comments, that’s not a CSV file. Each line is the Python representation of a list of numbers.
You can process it by reading each line and calling
ast.literal_eval() to convert it to a list.
import ast with open("filelocation") as f: freq_list = [ast.literal_eval(line) for line in f if line] frequencies = pd.Dataframe(freq_list)