Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/289.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 如何将列表中的大写单词转换为小写?_Python - Fatal编程技术网

Python 如何将列表中的大写单词转换为小写?

Python 如何将列表中的大写单词转换为小写?,python,Python,我得到了一个包含850行关于某个主题的不同问题的文本文件 都是用小写字母写的 我的最终目标是有一个文本文件,其中所有内容都是用大写字母写的,除了停止词和问题开头的单词 现在,我只是不知道如何将找到的单词转换成小写 # List of Stopwords import os import codecs # open working directory stopwords = open("C:\\Python Project\\Headings Generator\\stopwords.t

我得到了一个包含850行关于某个主题的不同问题的文本文件

都是用小写字母写的

我的最终目标是有一个文本文件,其中所有内容都是用大写字母写的,除了停止词和问题开头的单词

现在,我只是不知道如何将找到的单词转换成小写

# List of Stopwords
import os
import codecs
# open working directory
stopwords = open("C:\\Python Project\\Headings Generator\\stopwords.txt", "r" ,encoding='utf8',errors="ignore")

stopwordsList = [(line.strip()).title() for line in stopwords]

questions = open("C:\\Python Project\\Headings Generator\\questionslist.txt", "r" ,encoding='utf8',errors="ignore")
questionsList = [(line.strip()).title().split() for line in questions]

for sentences in questionsList:
    for words in sentences:
       if words in stopwordsList:
#How to replace the found word with a lowercase version of it?


多谢各位

在python中,可以使用内置函数将单词转换为小写,如下所示:

string = 'WORD'
string.lower()

如果字符串都是大写(WORD),它将变成(WORD),如果它包含大写和小写(WORD),它也将变成(WORD)

Python字符串有一个内置的
string.lower()
方法以小写形式返回字符串(还有一个
string.upper()
方法和
string.swapcase())
method,在所需的情况下都返回字符串)

使用string方法
string.lower()

从:

例如:

>“你好,世界”。下()
“你好,世界”
>>>“你好,世界”。下()
“你好,世界”
在代码中:

#停止词列表
导入操作系统
导入编解码器
#打开工作目录
stopwords=open(“C:\\Python项目\\标题生成器\\stopwords.txt”,“r”,encoding='utf8',errors=“ignore”)
stopwordsList=[(line.strip()).title()表示stopwords中的行]
问题=打开(“C:\\Python项目\\标题生成器\\questionslist.txt”,“r”,encoding='utf8',errors=“ignore”)
问题列表=[(line.strip()).title().split()用于问题行]
对于问题列表中的句子:
对于句子中的单词:
如果stopwordsList中的单词:
words=words.lower()

我还告诉你另一个

你必须使用
string.upper()
string.lower()
这就像单词一样简单。lower()嘿,谢谢你的帮助。我试过这样做,但当我打印问题列表时,似乎停止字未被触动。我能看看stopwordlist吗?问题是它没有保存在stopwordsList中。可能是因为这个字符串在一个列表中?创建一个新的列表,然后如果单词是大写的,将其调低,然后将其追加到列表中,否则直接追加就行了?这是一个句子,我正在检查stopwords以将它们转换成小写形式['What'、'Men'S'、'Body'、'Wash'、'Snoots'、'Best']、['What'、'is'、'Best'、'Body'、'Wash'],您可以创建一个字符串变量,在列表完成后,您可以执行
您的字符串\u var.join(您的\u列表\var)
您将获得一个字符串使用enumerate为insert方法获取索引,您认为如何?
str.lower()
Return a copy of the string with all the cased characters 4 converted to lowercase.

The lowercasing algorithm used is described in section 3.13 of the Unicode Standard.