# Python实际应用,处理字幕文件实现句首字母大写。读取文件lower.txt,实现句首字母大写,将处理结果输出到Upper.txt文件
infile = open("lower.txt", "r") # 打开输入文件
outfile = open("Upper.txt", "w") # 打开输出文件
flag = True # 需要处理首字母大写的行标志,默认首行处理
for line in infile.readlines(): # 以行为单位读取文件全部内容
if flag:
line = line.capitalize() # 将字符串的首字母变为大写
print(line, end='') # print中的默认值 sep=" ",end="\n"
outfile.write(line)
str = line.strip('\n') # 删除 str 字符串中开头、结尾处的换行符
str = line.strip() # 删除 str 字符串中开头、结尾处的空格,避免误判
flag = str.endswith(('.', '?', '!')) # 判断字符串是否以指定后缀结尾
infile.close() # 关闭打开的文件
outfile.close() # 关闭打开的文件,让文件内容写入磁盘文件