- 1、本文档共50页,可阅读全部内容。
- 2、有哪些信誉好的足球投注网站(book118)网站文档一经付费(服务费),不意味着购买了该文档的版权,仅供个人/单位学习、研究之用,不得用于商业用途,未经授权,严禁复制、发行、汇编、翻译或者网络传播等,侵权必究。
- 3、本站所有内容均由合作方或网友上传,本站不对文档的完整性、权威性及其观点立场正确性做任何保证或承诺!文档内容仅供研究参考,付费前请自行鉴别。如您付费,意味着您自己接受本站规则且自行承担风险,本站不退款、不进行额外附加服务;查看《如何避免下载的几个坑》。如果您已付费下载过本站文档,您可以点击 这里二次下载。
- 4、如文档侵犯商业秘密、侵犯著作权、侵犯人身权等,请点击“版权申诉”(推荐),也可以打举报电话:400-050-0827(电话支持时间:9:00-18:30)。
查看更多
* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * 以命令行方式运行python程序: Windows下,Win+R 键,可以弹出左边“运行”窗口,敲cmd确定,就能弹出右边 cmd窗口(命令行窗口) Mac上相应操作,是从launchPad里面 启动“终端” 在命令行窗口,敲 python XXX.py就可以运行XXX.py 安装Python 要在命令行方式启动python,就要在安装的时候勾上 不行就卸载重装 命令行参数 如果编写了一个程序 hello.py,功能是合并两个文件 希望在命令行敲 python hello.py a1.txt a2.txt 就能完成把a2.txt合并到a1.txt上面。 hello.py运行时,如何知道 要处理的文件是 a1.txt和a2.txt呢? a1.txt, a2.txt 都是“命令行参数”。因此程序内应该有获得命令行参数的方法 * 命令行参数 程序 hello.py如下: import sys for x in sys.argv: print(x) 命令行参数 程序 hello.py如下: import sys for x in sys.argv: print(x) 在命令行窗口以如下方式运行该程序,假设程序存为 hello.py: python hello.py this is hello world 则在程序中 sys.argv[0] 就是 hello.py sys.argv[1] 就是 this sys.argv[2] 就是 is sys.argv[3] 就是 hello world * 输出结果: hello.py this is hello world 程序以命令行运行时的当前文件夹 程序以命令行方式启动时,当前文件夹就是命令提示符表示的文件夹,而不是python程序文件所在的文件夹。 c:/tmp5/test/t.py import os print(os.getcwd()) * 命令提示符 文件处理实例 信息科学技术学院 郭炜 美国纪念碑谷 程序1: 统计文章中的单词词频 程序名:countfile.py 用命令行方式启动该程序: python countfile.py 源文件 结果文件 例如: python countfile.py a1.txt r1.txt python countfile.py c:\tmp\a4.txt d:\tmp\r4.txt 对 源文件 进行单词词频(出现次数)分析,分析结果写入 结果文件,单词按照字典序排列 * 程序1: 统计文章中的单词词频 文章文件 a1.txt 的格式: When many couples decide to expand their family, they often take into consideration the different genetic traits that they may pass on to their children. For example, if someone has a history of heart problems, they might be concerned about passing that on to their children as well. Treacher Collins syndrome, or TCS,?is a rare facial disfigurement that greatly: slows: the development of bones and other tissues that make up the human face. As a result, most people living?with TCS have?underdeveloped cheek bones, a small jaw, and an undersized chin. * 程序1: 统计文章中的单词词频 统计的结果结果文件 r1.txt 格式 a 8 about 2 an 1 and 4 are 1 around 1 as 2 backlash 1 be 4 * 程序1: 统计文章中的单词词频 思路: 命令行参数sys.argv[1]就是源文件,sys.argv[2]就是结果文件。 要从a1.txt中分割出单词,然后用字典记录单词的出现频率。 分割单词时的分隔字符多种多样,因此要统计a1.txt中出现了哪些非字母的字符,非字母的字符都是分隔串。 要用 re.split() 来分
您可能关注的文档
- Python程序设计 7. 组合数据类型(3)字典和集合.ppt
- Python程序设计 8. 计算思维.ppt
- Python程序设计 10. Python正则表达式.ppt
- Python程序设计 11. 玩转python生态.ppt
- Python程序设计 12. python数据库编程.ppt
- Python程序设计 13. numpy和pandas数据分析.ppt
- Python程序设计 14. python图像处理_一些补充.ppt
- Python程序设计 14.用matplotlib展示数据.ppt
- Python程序设计 15. 爬虫.ppt
- Python程序设计 16. 面向对象程序设计.pptx
文档评论(0)