简体   繁体   English

如何通过在 python 中迭代句子中的单词来创建表?

[英]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彼得 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.

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