1

How would one split line into an array of words and convert them to lowercase with Python? I am working with a TXT file. Below is my work thus far:

file_data = []

# ------------ Add your code below --------------

# We need to open the file

with open('/dsa/data/all_datasets/hamilton-federalist-548.txt', 'r') as file: 
# For each line in file
    for line in file:
        line = line.strip()
        split_line = line.split(' ')
        file_data.append(split_line)
        print(split_line)   
        
# We want to split that line into an array of words and convert them to lowercase

    
# [x.lower() for x in ["A","B","C"]] this example code will covert that list of letters to lowercase

print(file_data.lower())
  • Does this answer your question? [Converting a String to a List of Words?](https://stackoverflow.com/questions/6181763/converting-a-string-to-a-list-of-words) – DYZ Jul 16 '21 at 01:17
  • I was able to convert the list into an array of words. How would I convert them to lowercase from here? The print(file_data.lower()) is not converting the uppercase strings into lower case. – user16443752 Jul 16 '21 at 01:22
  • Convert the line to the lower case and _then_ split. – DYZ Jul 16 '21 at 01:26

1 Answers1

0

You have to convert them before adding them to the file_data list.

So instead of:

split_line = line.split(' ')

Try this:

split_line = [i.lower() for i in line.split(' ')]
U13-Forward
  • 69,221
  • 14
  • 89
  • 114