增补博客 第三篇 python 英文统计
1.增补博客 第二篇 python 谢宾斯基三角型字符分形图形输出
2.增补博客 第三篇 python 英文统计
3.增补博客 第四篇 python 中文级联菜单4.增补博客 第五篇 python 电子算盘5.增补博客 第六篇 python 电子算盘6.增补博客 第七篇 python 比较不同Python图形处理库或图像处理库的异同点7.增补博客 第八篇 python 中国大学排名数据分析与可视化8.增补博客 第九篇 python 图书评论数据分析与可视化9.增补博客 第十篇 python 函数图形绘制10.增补博客 第十一篇 python 分段函数图形绘制11.增补博客 第十二篇 python大作业小说阅读器(1)爬取12.增补博客 第十三篇 python大作业小说阅读器(2)爬取13.增补博客 第十四篇 python大作业小说阅读器(3)显示文字函数14.增补博客 第十五篇 python大作业小说阅读器(4)html页面15.增补博客 第十七篇 python 模拟页面调度LRU算法16.增补博客 第十八篇 python 杨辉三角形17.增补博客 第二十篇 python 筛法求素数18.增补博客 第二十一篇 python 查找鞍点19.增补博客 第二十四篇 python 正整数的因子展开式20.增补博客 第二十二篇 python 牛顿迭代法21.增补博客 第二十三篇 python 对比Python中的列表、元组、字典、集合、字符串等之间异同22.增补博客 第十九篇 python 爬楼梯23.增补博客 第一篇 python 简易带参计算器24.增补博客 第十六篇 python 排列组合序列25.增补博客 第二十五篇 python 列举说明Python同Java及C++的不同之处编写程序实现对特定英文文章(文本文件)的单词数和有效行数的统计,其中要求空行不计数;
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 | def count_words_and_lines(file_path): word_count = 0 line_count = 0 # 打开文件 with open (file_path, 'r' ) as file : for line in file : line = line.strip() # 去除首尾空白字符 # 空行不计数 if line: line_count + = 1 # 使用空格分割单词 words = line.split() word_count + = len (words) return word_count, line_count # 输入要处理的文本文件路径 file_path = input ( "请输入文本文件路径:" ) try : # 统计单词数和有效行数 word_count, line_count = count_words_and_lines(file_path) print (f "单词数: {word_count}" ) print (f "有效行数: {line_count}" ) except FileNotFoundError: print ( "找不到指定的文件,请确认路径是否正确。" ) except Exception as e: print (f "发生错误: {e}" ) |
合集:
python
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· Manus重磅发布:全球首款通用AI代理技术深度解析与实战指南
· 被坑几百块钱后,我竟然真的恢复了删除的微信聊天记录!
· 没有Manus邀请码?试试免邀请码的MGX或者开源的OpenManus吧
· 园子的第一款AI主题卫衣上架——"HELLO! HOW CAN I ASSIST YOU TODAY
· 【自荐】一款简洁、开源的在线白板工具 Drawnix