Laxmikant
Laxmikant

Reputation: 2216

Python - Parse a list of string formatted list of strings

I am having problem in parsing list of strings in the below format. This is actually a pandas dataframe:

def parse_text_to_list(row):
    print(row) # eval is not working as inner strings are missing the quotes
    return row

df.apply(parse_text_to_list)  

output

# printed only one row to simplify the question
['[[str1, str2], [str1, a long text], ..., [strn, strx]]']

But Want to convert it to a pure python list like:

[["str1", "str2"], ["str1", "a long text"], ... ["strn", "strx"]]

@Negative markers - let me know the reason

Upvotes: 0

Views: 201

Answers (2)

Ariel Catala Valencia
Ariel Catala Valencia

Reputation: 112

If you want print each row as a list you can use:


def parse_text_to_list(row):
    print(row.tolist())
    return row

But if you want convert each row to List,you can use directly:

df.values.tolist()

This questions is already resolved here

Upvotes: 0

Epsi95
Epsi95

Reputation: 9047

You can try regular expression with literal_eval to get the list

import re
import ast

l = ['[[str1, str2], [str1, a long text], [strn, strx]]']

output = ast.literal_eval(re.sub(r'([^\[\],\s][^\[\],]+[^\[\],\s])', r'"\1"', l[0]))

print(output)
[['str1', 'str2'], ['str1', 'a long text'], ['strn', 'strx']]

Upvotes: 2

Related Questions