如何使用Python将行拆分为单词数组并将其转换为小写



如何使用Python将行拆分为单词数组并将其转换为小写?我正在处理一个TXT文件。以下是我迄今为止的工作:

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

在将它们添加到file_data列表之前,必须对它们进行转换。

所以不是:

split_line = line.split(' ')

试试这个:

split_line = [i.lower() for i in line.split(' ')]

最新更新