简体   繁体   中英

Find sentence and paragraph with Python

I have following format of data:

[1956, Jon's story, He sold his soul in 1987, 200]  
[1960, Mary's story, "She liked her soul, but decided to sold it anyway.", 250]  
[1963, "Alice and Peter story, with a twist", "Peter said "Your soul is mine!" and tried to sold it, but Alice had no soul and killed him.", 500]

I want to split this to

[1956, 1960, 1963]  
['Jon's story', 'Mary's story','Alice and Peter story, with a twist']  
['He sold his soul in 1987','She liked her soul, but decided to sold it anyway.','Peter said "Your soul is mine!" and tried to sold it, but Alice had no soul and killed him.']  
[200,250,500]

So far I've done this

import re
data = [[1956, "Jon's story", "He sold his soul in 1987", 200],
        [1960, "Mary's story", "She liked her soul, but decided to sold it anyway.", 250],
        [1963, "Alice and Peter story, with a twist", "Peter said 'Your soul is mine!' and tried to sold it, but Alice had no soul and killed him.", 500]]
for row in data:
    line = str(row)
    sentence = re.split(r',', line)

But this way it takes into account comma separation inside " ". How can I avoid it?

So this can be solved by using zip instead of re, look below at the code and see how it works. m4,m3,m2,m1 will be the lists with the values you needed

    data = [[1956, "Jon's story", "He sold his soul in 1987", 200],
    [1960, "Mary's story", "She liked her soul, but decided to sold it anyway.", 250],
    [1963, "Alice and Peter story, with a twist", "Peter said 'Your soul is mine!' and tried to sold it, but Alice had no soul and killed him.", 500]]
    m4, m3 ,m2,m1 = map(list, zip(*data))

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM