-2

我得到了一个包含 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?

非常感谢!

4

3 回答 3

0

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

string = 'WORD'
string.lower()

如果你的字符串都是大写(WORD),它会变成(word),如果它包含大写和小写(WoRd),它也会变成(word)

于 2020-06-23T11:57:35.147 回答
0

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

于 2020-06-23T11:58:36.407 回答
0

使用字符串方法string.lower()
来自文档

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.

例子 :

>>> 'HELLO WORLD'.lower()
'hello world'
>>> 'HeLLo WorLD'.lower()
'hello world'

在您的代码中:

# 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:
           words = words.lower()

我也给你这个其他帖子

于 2020-06-23T11:58:48.093 回答