江弘毅的作业一

代码

作业一

import linecache   

file = open('C:/Users/jhy/作业一素材.txt', 'r', encoding='utf-8')

text = file.read()

the_line = linecache.getline('C:/Users/jhy/作业一素材.txt', 52)

words = the_line.split()

last_word = words[-1].rstrip('.')  

print('lastword:{},length{}'.format(last_word,len(last_word)))

 

作业一附加

file1 = open('C:/Users/jhy/作业一附加选做题素材.txt', 'r', encoding='utf-8')

text1 = file1.read()

import re

sentences = re.split('。|!|?', text1)

sentences = list(filter(None, sentences))

last_sentence = sentences[-2]

print("last sentence:",last_sentence)

sentence_without_commas = last_sentence.replace(',', '')

length = len(sentence_without_commas)

print("Length:", length)

 

结果

解释