您可以在re 模块中使用split() 方法:
import re
s = ['t__f326ea56',
'foo\tbar\tquax',
'some\ts\tstring']
new_data = [re.split("\\t", i) for i in s]
s1 = new_data[0][0]
s2, s3, s4 = map(list, zip(*new_data[1:]))
输出:
s1 = 't__f326ea56'
s2 = ['foo', 'some']
s3 = ['bar', 's']
s4 = ['quax', 'string']
编辑:
对于列表列表:
s = [['t__f326ea56', 'foo\tbar\tquax', 'some\ts\tstring'], ['second\tbar\tfoo', 'third\tpractice\tbar']]
new_s = [[re.split("\\t", b) for b in i] for i in s]
new_s现在商店:
[[['t__f326ea56'], ['foo', 'bar', 'quax'], ['some', 's', 'string']], [['second', 'bar', 'foo'], ['third', 'practice', 'bar']]]
转置new_s中的数据:
new_s = [[b for b in i if len(b) > 1] for i in new_s]
final_s = list(map(lambda x: zip(*x), new_s))
final_s 现在将以您想要的原始方式存储数据:
[[('foo', 'some'), ('bar', 's'), ('quax', 'string')], [('second', 'third'), ('bar', 'practice'), ('foo', 'bar')]]