不同栏中的第 Table 段

Paragraph to Table in Different Columns

我想创建一个程序,如果我在文本区域中插入一个段落,我希望它的某些部分放在不同列的 table 中。例如语句是:

我叫詹姆斯·奥尔森。我21岁了。我是一名医生。我住在伦敦培根街的坎特维尔。

那么 table 应该自动看起来像:


|名称 |年龄 |专业|地区名称 |街道名称 |面积 |


詹姆斯 | 21 |医生 |坎特维尔 |培根街 |伦敦 |


我也想知道哪种语言最好 - Python 或 Java。

是的当然可以这样做,我个人更喜欢Python来完成这项工作。

我已经编写了代码,它不是最好的或最有效的代码,但肯定会完成工作,但我的代码中有一个陷阱。仅当您句子的 序列和模式 相同时,它才会起作用。该模式应与您在 示例 .

中提供的模式完全相同

如果您希望代码适用于多个句子,只需稍微更改代码中的循环即可。

import pandas as pd

my_sent = "My name is James Olson. I am 21 years old. I am a doctor. I live in Canterville, Bacon Street, London."
my_words = my_sent.split()

my_stopwords = ['My', 'name', 'is', 'I', 'am', 'years', 'old.', 'I', 'am', 'a', 'I', 'live', 'in',]

cleaned_stopwords = []
useful_words = []

for temp in my_stopwords:
    cleaned_stopwords.append(temp.lower().strip())

for word in my_words:
    if word.lower().strip() not in cleaned_stopwords:
        useful_words.append(word.title().strip(".").strip(","))

name = useful_words[0] + " " + useful_words[1]
street = useful_words[5] + " " + useful_words[6]

useful_words.pop(0)
useful_words.pop(0)
useful_words.insert(0, name)
useful_words.pop(4)
useful_words.pop(4)
useful_words.insert(4, street)

all_columns = ["Name", "Age", "Profession", "Area Name", "Street Name", "Area"]
my_df = pd.DataFrame([useful_words], columns = all_columns)

输出:

           Name  Age  Profession   Area Name    Street Name    Area
0   James Olson   21      Doctor Canterville   Bacon Street  London