我有以下格式的数据:
[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]我想把这个分成
[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]到目前为止,我已经这样做了
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)但这种方式会考虑到“”内部的逗号分隔。我怎么才能避免呢?
发布于 2017-08-18 23:08:43
所以这可以通过使用zip而不是re来解决,看看下面的代码,看看它是如何工作的。m4、m3、m2、m1将是包含所需值的列表
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))https://stackoverflow.com/questions/45759345
复制相似问题