邹宇的作业一

代码


import linecache 
#作业一   
file = open('D:/python homework/数据获取与处理/作业一素材.txt', 'r', encoding='utf-8')
text = file.read()
the_line = linecache.getline('D:/python homework/数据获取与处理/作业一素材.txt', 52)
words = the_line.split()
last_word = words[-1].rstrip('.')  # get the last sentence
print("last_word:",last_word)
length_word = len(last_word)
print("Length of word:", length_word)

#作业一附加
file1 = open('D:/python homework/数据获取与处理/作业一附加选做题素材.txt', 'r', encoding='utf-8')
text1 = file1.read()
import re
sentences = re.split('。|!|?', text1)
sentences = list(filter(None, sentences))
last_sentence = sentences[-2]  # get the last sentence
print("last_sentence:",last_sentence)
#去除文字中的逗号
sentence_without_commas = last_sentence.replace(',', '')
length = len(sentence_without_commas)
print("Length of sentence:", length)

结果

结果截图

解释