Find list within a string

Viewed 150

I see lots of examples of finding strings within strings, or finding strings within lists, but how do I find a list within a string. For example I have a csv file with columns of data and the last column is either a string or sometimes it is a list. Here is a subset of data showing just the last 3 columns.

TRUE, 93877, S26476961
TRUE, 93878, ['S26489167', 'S26492524']
FALSE, 93879, S26476962
FALSE, 93880, ['S26489168', 'S26492527', 'S26492528']

At first I was trying to parse each entire line of the csv file at the comma but it parses the commas within the list (creating extra columns). I just want the list to be recognized as a singular piece of data so I can work with it as a list of 'n' items. @TemporalWolf 's comment helps greatly because if I use the csv module (specifically csv.reader) as so ...

reader = csv.reader(inFile)
for row in reader:
    print(row)

It keeps the list in a single column. Now the problem remains that it is just a string. In other words, row[n][0] returns the left bracket ([) but I want to make that into a list.

3 Answers
Related