Python 获取字母表下半角的错误

Python 获取字母表下半角的错误,python,string,list,split,Python,String,List,Split,我有大量的电影对话数据:- 康奈尔电影对话。 我试图把它们转换成小写字母 def clean_text(text): text = text.lower() text = re.sub(r"i'm", "i am", text) text = re.sub(r"he's", "he is", text) text = re.sub(r"she's", "she is", text) text = re.sub(r"that's", "that is

我有大量的电影对话数据:- 康奈尔电影对话。 我试图把它们转换成小写字母

    def clean_text(text):
    text = text.lower()
    text = re.sub(r"i'm", "i am", text)
    text = re.sub(r"he's", "he is", text)
    text = re.sub(r"she's", "she is", text)
    text = re.sub(r"that's", "that is", text)
    text = re.sub(r"what's", "what is", text)
    text = re.sub(r"where's", "where is", text)
    text = re.sub(r"\'re", "are", text)
    text = re.sub(r"\'d", "would", text)
    text = re.sub(r"won't", "will not", text)
    text = re.sub(r"can't", "cannot", text)
    text = re.sub(r"[-()\"#/@;:<>{}+=-|.?,]", "", text)
    text = re.sub(r"\'ll", "will", text)
    text = re.sub(r"\'ve", "have", text)
    return text

#cleaning the questions
clean_questions = []
for question in questions:
    clean_questions.append(clean_text(question))
我得到了以下回调:

Traceback (most recent call last):

  File "<ipython-input-17-4733a5502cb3>", line 3, in <module>
    clean_questions.append(clean_text(question))

  File "<ipython-input-16-a9a9890808b2>", line 2, in clean_text
    text = text.lower()

AttributeError: 'list' object has no attribute 'lower'

任何关于我做错了什么以及如何改正的建议都将不胜感激!!!谢谢

Str.lower不适用于整个列表,而适用于单独的字符串。您可以通过执行以下操作来修复它:

for i in range(len(text)): text[i].lower()

Lower是一个字符串方法,不能用于列表。使用循环遍历列表并降低每个元素。无论如何,即使列表中没有显示问题,我也会回答:

这取决于列表,如果它是嵌套列表,我认为这是因为错误,即:

l=[['ABC','XYZ'],'BlA']
它不起作用,因为有列表作为元素,所以将其展平:

x=[]
for i in l:
    if type(i) is list:
        x.extend(i)
    else:
        x.append(i)
现在:

是:

那么你的x代码就行了,OTOH:

['A','B','C']
表示所有字符串,它也会按预期工作。

但问题看起来如何???,如果这篇文章对用户来说是有用的信息,那么可能会得到答案。看起来问题属于列表类型。显示示例问题撇开错误不谈,re.subr[-\/@;:{}+=-\.?,],文本可能无法执行您希望它执行的操作,因为中间-。
['ABC','XYZ','BlA']
['A','B','C']