如何用Python实现任一个英文的纯文本文件

Python011

如何用Python实现任一个英文的纯文本文件,第1张

import re

file_name = 'test.txt'

lines_count = 0

words_count = 0

chars_count = 0

words_dict  = {}

lines_list   = []

with open(file_name, 'r') as f:

    for line in f:

        lines_count = lines_count + 1

        chars_count  = chars_count + len(line)

        match = re.findall(r'[^a-zA-Z0-9]+', line)

        for i in match:

            # 只要英文单词,删掉其他字符

            line = line.replace(i, ' ')

        lines_list = line.split()

        for i in lines_list:

            if i not in words_dict:

                words_dict[i] = 1

            else:

                words_dict[i] = words_dict[i] + 1

print 'words_count is', len(words_dict)

print 'lines_count is', lines_count

print 'chars_count is', chars_count

for k,v in words_dict.items():

    print k,v

python是一款应用非常广泛的脚本程序语言,谷歌公司的网页就是用python编写。python在生物信息、统计、网页制作、计算等多个领域都体现出了强大的功能。python和其他脚本语言如java、R、Perl 一样,都可以直接在命令行里运行脚本程序。工具/原料

python;CMD命令行;windows操作系统

方法/步骤

1、首先下载安装python,建议安装2.7版本以上,3.0版本以下,由于3.0版本以上不向下兼容,体验较差。

2、打开文本编辑器,推荐editplus,notepad等,将文件保存成 .py格式,editplus和notepad支持识别python语法。

脚本第一行一定要写上 #!usr/bin/python

表示该脚本文件是可执行python脚本

如果python目录不在usr/bin目录下,则替换成当前python执行程序的目录。

3、编写完脚本之后注意调试、可以直接用editplus调试。调试方法可自行百度。脚本写完之后,打开CMD命令行,前提是python 已经被加入到环境变量中,如果没有加入到环境变量,请百度

4、在CMD命令行中,输入 “python” + “空格”,即 ”python “;将已经写好的脚本文件拖拽到当前光标位置,然后敲回车运行即可。

python 大蟒

双语例句

Unfortunately, the standard Python 2.0 XML package does not contain a validating

parser.

不幸的是,标准Python2.0XML包并不包括验证型语法分析器。

Third one is, it may depend on which version of Python I'm running.

第三点就是,这可能会和我,用的Python的版本有关系。

We had a Burmese python abandoned in a house that was being repossessed.

他说:“以前我们在一所房子里发现过一条缅甸巨蟒,后来有幸被领养了。

This new .chm file includes documentation on the Python language and standard library.

这个新的chm文件包括Python语言及标准库的文档。