我一直在从事WhatsApp Chat的python数据可视化项目。我有一根这样的绳子。
line = '[14/11/18, 2:47:26 PM] Chaitanya: Yeah, Lets go to the movies to night'我想把它分解成这样。
['[14/11/18, 2:47:26 PM]', 'Chaitanya: Yeah, Lets go to the movies to night']我尝试过使用split()函数,但我似乎无法得到完全相同的东西。此外,第一次字段将发生变化,因此该字段的长度可能不会每次都相同。
我会提供一些帮助。谢谢。
发布于 2020-05-14 13:13:31
[line[:line.find(']')+1],line[line.find(']')+2:]]顺便说一句:使用帮助器变量来查找结果应该更快,当您执行DataViz时,这可能对您更好:
f=line.find(']')
[line[:f+1],line[f+2:]]来自时间的结果:
>>> import timeit
>>> timeit.timeit("line = '[14/11/18, 11:47:26 PM] Chaitanya: Yeah, Lets go to the movies [to] night'; [line[:line.find(']')+1],line[line.find(']')+2:]]")
0.33965302700016764
>>> timeit.timeit("line = '[14/11/18, 11:47:26 PM] Chaitanya: Yeah, Lets go to the movies [to] night'; f=line.find(']'); [line[:f+1],line[f+2:]]")
0.21619235799971648发布于 2020-05-14 13:08:02
试试这个:
r = line.split(']', 1)
r[0] += ']'发布于 2020-05-14 13:12:17
line = '[14/11/18, 2:47:26 PM] Chaitanya: Yeah, Lets go to the movies to night'
reslist =line.split(']',1)
reslist[0] += "]" # needed because split removes delimiter
reslist[1] = reslist[1].lstrip()
print(reslist) # ['[14/11/18, 2:47:26 PM]', 'Chaitanya: Yeah, Lets go to the movies to night']https://stackoverflow.com/questions/61798102
复制相似问题