What is the best way to read in a line of numbers from a file when they are presented in a format like this:
[1, 2, 3 , -4, 5]
[10, 11, -12, 13, 14 ]
Annoyingly, as I depicted, sometimes there are extra spaces between the numbers, sometimes not. I've attempted to use CSV
to work around the commas, but the brackets and the random spaces are proving difficult to remove as well. Ideally I would append each number between the brackets as an int
to a list
, but of course the brackets are causing int()
to fail.
I've already looked into similar solutions suggested with Removing unwanted characters from a string in Python and Python Read File, Look up a String and Remove Characters, but unfortunately I keep falling short when I try to combine everything.