0

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

3
  • add your input dataframe to Q Commented Feb 23, 2021 at 14:40
  • @SreeramTP - Can you help more on it? Commented Feb 23, 2021 at 14:40
  • If you can add few rows of input dataframe to your Q as text, sure I can try to help Commented Feb 23, 2021 at 14:41

2 Answers 2

2

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']]
Sign up to request clarification or add additional context in comments.

2 Comments

Perfect, Thanks very much. It worked like charm. but wondering whats happening in the regex part. Can you please explain a bit.
It is checking if anything starting with anything except [ ] space , then in between anything except [ ] , more than one times and ending with anything except [ ] space ,
0

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

1 Comment

No, it does not answer my question

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.