一、读取txt文件
内置函数open,接受文件名,返回一个文件对象
fin=open('words.txt')
for line in fin:
word=line.strip()
if len(word)>20:
print(word)
else:
pass
二、去除首尾空格字符
(1)strip函数
str.strip(char) 移除字符串头尾指定的字符。
str.strip() 清除字符串头尾的空格
PS: 字符串不能随意修改
三、遍历列表并建立一个新列表
for循环遍历fin这个文件对象,将去掉单词首尾空格的新字符串增加到新列表word_list中。
fin=open('words.txt')
word_list=[]
for line in fin:
word=line.strip()
word_list.append(word)