-2

I got a text file with 850 lines of different questions about a topic.

All are written in lowercase.

My final goal is to have a text file, where everything is written in uppercase except the stopwords AND words the Questions start with.

For now, I just don't know how I can convert the found words into lowercase

# 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?

Thank you very much!

3 Answers3

0

In python you can turn a word into lowercase using a built-in function just as follows:

string = 'WORD'
string.lower()

if you string is all upercase (WORD) it will become (word) and if it contains uppers and lowers (WoRd) it will also become (word)

RaoufM
  • 525
  • 5
  • 22
  • The issue with that is that its not getting saved in the stopwordsList. Maybe because this string is in a List? – Pythontrator Jun 23 '20 at 12:19
  • Create a new list and then if the word is in uppercase turn it to lower then append it to the list , else just append it straight away – RaoufM Jun 23 '20 at 12:28
  • Append would work? This is a sentence where i´m checking for stopwords to convert them to lower case ['What', "Men'S", 'Body', 'Wash', 'Smells', 'The', 'Best'], ['What', 'Is', 'The', 'Best', 'Body', 'Wash'], – Pythontrator Jun 23 '20 at 12:30
  • You can create a variable which is a string and then after your list is finished you can do `your_string_var.join (your_list_var)` and you will get a string – RaoufM Jun 23 '20 at 12:34
  • what do you think about using enumerate to get a index for the insert method? – Pythontrator Jun 23 '20 at 13:14
  • I dont know .. I think it may work – RaoufM Jun 23 '20 at 13:17
0

Python Strings have an in-built string.lower() method that returns the String in lower case (there's also a string.upper() method as well as a string.swapcase() method, both return a string in the desired case)

fesieg
  • 467
  • 1
  • 4
  • 14
0

use the string method string.lower().
from the doc:

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.

Example :

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

In your code:

# 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()

I also give you this other post

Lwi
  • 354
  • 4
  • 10