It depends on the character 'with which the items in your list are quoted. Using this information, it is separated only by commas, not followed or preceding this character, using a regular expression:
import re
import pandas as pd
import io
text = """TRUE, 93877, S26476961
TRUE, 93878, ['S26489167', 'S26492524']
FALSE, 93879, S26476962
FALSE, 93880, ['S26489168', 'S26492527', 'S26492528']"""
with io.StringIO(text) as f:
for line in f:
print(re.split("(?<!'), (?!')", line.strip()))
with io.StringIO(text) as f:
print(pd.read_csv(f,
header=None,
sep="(?<!'), (?!')",
engine='python'))
Edit:
If you are using python2, you will need to convert the text to unicode (putting a character uin front of the text) in order to be able to use io.StringIO:
import re
import pandas as pd
import io
text = u"""TRUE, 93877, S26476961
TRUE, 93878, ['S26489167', 'S26492524']
FALSE, 93879, S26476962
FALSE, 93880, ['S26489168', 'S26492527', 'S26492528']"""
with io.StringIO(text) as f:
for line in f:
print(re.split("(?<!'), (?!')", line.strip()))
with io.StringIO(text) as f:
print(pd.read_csv(f,
header=None,
sep="(?<!'), (?!')",
engine='python'))
Edit 2:
', :
import ast
import re
with io.StringIO(text) as f:
for line in f:
parts = re.split(", (?=\[)", line.strip())
line = []
for part in parts:
if all(char in part for char in ('[]')):
line.append(ast.literal_eval(part))
else:
line += part.split(", ")
print(line)
, , :
- , , , . ,
list ast.literal_eval . - .
, .
, .