[英]How to create a table by iterating words in sentence in python?
For the example given below, I would like to create a table for the list of 'Directors' and 'Stars'.对于下面给出的示例,我想为“导演”和“明星”列表创建一个表。 The idea is to iterate the sentences, detect words which exist between word 'Director:' and 'Stars:' and put into respective cells.
这个想法是迭代句子,检测单词'Director:'和'Stars:'之间存在的单词并放入各自的单元格中。
The sentence.这句话。
Director:
Peter
Jackson,
John
Marsh
Stars:
Elijah
Wood,
Ian
McKellen,
Orlando
Bloom,
Sean
Bean
Full list of words https://justpaste.it/3qjc4完整的单词列表https://justpaste.it/3qjc4
Table to create要创建的表
Director![]() |
Stars![]() |
---|---|
Peter Jackson![]() |
Elijah Wood![]() |
John Marsh![]() |
Ian McKellen![]() |
Orlando Bloom![]() |
|
Sean Bean![]() |
Try this:尝试这个:
import pandas as pd
l=[]
with open('yourtxtfile.txt') as f:
for i in f:
l.append(i)
l=[i.replace('\n', '') for i in l]
Director=l[1:l.index('Stars')]
Stars=l[l.index('Stars')+1:]
for i in range(len(Director)-len(Stars)):
Director.append('')
df=pd.DataFrame({'Director':Director, 'Stars':Stars})
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.