Python 如何将列表对象作为字符串传递?

Python 如何将列表对象作为字符串传递?,python,string,Python,String,我正在使用Textblob分析一本书的全文,通过单独分析句子来聚合章节的语气。我有一个脚本,可以将章节转换为单个句子的列表,但我无法找到一种方法将这些列表对象作为字符串传递给Naive Bayes分析器,因为它只需要字符串输入 到目前为止,我只尝试将整个列表作为参数传递,但它总是给我相同的错误 TypeError: The `text` argument passed to `__init__(text)` must be a string, not <class 'list'>

我正在使用Textblob分析一本书的全文,通过单独分析句子来聚合章节的语气。我有一个脚本,可以将章节转换为单个句子的列表,但我无法找到一种方法将这些列表对象作为字符串传递给Naive Bayes分析器,因为它只需要字符串输入

到目前为止,我只尝试将整个列表作为参数传递,但它总是给我相同的错误

 TypeError: The `text` argument passed to `__init__(text)` must be a string, 
 not <class 'list'>
我的列表如下所示:

sentences = ['Maria was five years old the first time she heard the word 
hello.\n', 'It happened on a Thursday.\n',]
                                         Line          Polarity Subjectivity Classification
0    Mariam was five years old the first time sh      0.175000   0.266667   Pos                                                 
1    It happened on a Thursday.                       0.000000   0.000000 Neu
如何修改此代码以接收整个句子列表并将输出作为数据帧传递?如果可能的话,我想要这样的东西:

sentences = ['Maria was five years old the first time she heard the word 
hello.\n', 'It happened on a Thursday.\n',]
                                         Line          Polarity Subjectivity Classification
0    Mariam was five years old the first time sh      0.175000   0.266667   Pos                                                 
1    It happened on a Thursday.                       0.000000   0.000000 Neu

你的意思是像这样构造一个数据帧吗。至少这是我从你的问题中理解的。 我假设你有一个句子列表,在运行分析器之前,我把它们连接到一个段落中

import pandas as pd
from textblob import TextBlob

from textblob.sentiments import NaiveBayesAnalyzer

df = pd.DataFrame(columns = ['Line','Polarity', 'Subjectivity' ,'Classification'])
sentences = ['Maria was five years old the first time she heard the word hello.\n', 'It happened on a Thursday.\n',]

blob = TextBlob("".join(sentences),analyzer=NaiveBayesAnalyzer())
for sentence in blob.sentences:
    df.loc[len(df)] = [str(sentence),sentence.polarity, sentence.subjectivity,sentence.sentiment.classification]
print(df)

除非
textblob
允许您传入字符串以外的任何内容,否则您必须手动将其映射到字符串上。您的意思是手动输入每个字符串?这是不可能的,因为有数千行这样的文字和读数需要记录。有没有一种方法可以使用在列表上运行的循环,将字符串一个接一个地输入textblob?请尝试下面的解决方案