### file1 = open("data.dat") ### file1 = open("data.dat") ### file3 = open("/courses/new-Computing/progs/data.dat") ### file4 = open("C:/texlive/2014/release-texlive.txt") ### #### 统计一个Python程序里几类字符的个数 ##def cnt_file(fname): ## digits = letters = spaces = others = 0 ## infile = open(fname, encoding="utf_8") ## for line in infile: ## for c in line: ## if c.isdigit(): ## digits += 1 ## elif c.isalpha(): ## letters += 1 ## elif c.isspace(): ## spaces += 1 ## else: ## others += 1 ## infile.close() ## print("In the file, there are:") ## print("\t", digits, "digits;") ## print("\t", letters, "letters;") ## print("\t", spaces, "blank chars;") ## print("\t", others, "other chars.") ### 用 cnt_file("some-others.py") 试验 ### 如果读入的文件里有中文,要看中文的编码格式,一般可以用 ### infile = open(fname, encoding="utf_8") 打开文件 ### 修改之后就可以处理 cnt_file("comp17-others2.py") #### 生成一个数据文件 ##outf = open("resfile1.dat", "w") ##outf.write("Generated integers:\n") ##for i in range(10): ## outf.write(", ".join([str(i**2 + j) for j in range(10)]) ## + '\n') ##outf.close() ### 设给定文件里保存着一批浮点数,要求读入做些浮点数做成一个表 ### 下面是简单定义 ##def read_floats(fname): ## infile = open(fname) ## return list(map(float, infile.read().split())) ## ##flist = read_floats("data.dat") ### 读入表中,而后就可以随意使用了 ### 缺点: 一次读入整个文件,如果数据很多,构造出的表很大 #### 希望做一个函数,每次调用返回文件里的一个浮点数 #### 定义一对函数,一个函数打开文件,另一个函数调用一次返回一个浮点数 #### 没有更多浮点数时后移函数返回 None ##infile = None ##nlist = [] ##crt = 0 ## ##def open_floats(fname): ## global infile ## infile = open(fname) ## ##def next_float(): # 缓冲式处理 ## global nlist, crt ## if crt == len(nlist): # 一行已经用完 ## line = infile.readline() ## if not line: # 整个文件已经处理完了 ## infile.close() ## return None ## nlist = line.split() ## crt = 0 ## crt += 1 ## return float(nlist[crt - 1]) # 使用方式: # 首先调用 open_floats(filename) 初始化相关功能 # 以后每次调用 next_float() 得到下一个浮点数 ####简单运行实例: ##open_floats("data.dat") ##for i in range(10): ## print(next_float()) ####输出属于区间 (20, 30) 的数,最后输出数据总数和输出数据的个数: ##open_floats("data.dat") ##num = 0 ##printed = 0 ##while True: ## x = next_float() ## if x == None: ## break ## num += 1 ## if 30 > x > 20: ## print(x) ## printed += 1 ## ##print("Read floats: ", num) ##print("Print floats: ", printed) #### 缺点:用了两个全局变量,功能可能受到干扰 #### 另一种实现 #### 其中使用了局部函数和局部变量 #### 返回局部定义的函数对象(实际上带着局部环境) ##def read_floats(fname): ## nlist = [] ## infile = open(fname) ## crt = 0 ## ## def next_float(): ## nonlocal nlist, crt ## if crt == len(nlist): # 一行已经用完 ## line = infile.readline() ## if not line: # 整个文件已经处理完了 ## infile.close() ## return None ## nlist = line.split() ## crt = 0 ## crt += 1 ## return float(nlist[crt - 1]) ## ## return next_float # 返回局部定义的函数对象 ### end of open_float ##nextf1 = read_floats("data.dat") ##for i in range(10): ## print(nextf1()) ##print("-------------") ## ##nextf2 = read_floats("datafile.dat") ##for j in range(10): ## print(nextf2()) ##print("-------------") ## ##for i in range(10): ## print(nextf1()) #### 用生成器实现类似功能 #### 返回文件中的浮点数“序列” ##def read_floats(fname): ## nlist = [] ## infile = open(fname) ## ## while True: ## line = infile.readline() ## if not line: # 整个文件已经处理完了 ## infile.close() ## return None ## nlist = line.split() ## for num in nlist: ## yield float(num) ###### ##n = 0 ##for x in read_floats("data.dat"): ## n += 1 ## if 3 < x < 6: ## print(x) ##print("Totally", n, "numbers in the file.") #### 统计一个文本文件里各单词出现的次数 #### 单词简单定义为空白字符分隔的连续非空白字符 ##def textstat (infname, statfile): ## worddict = {} ## textfile = open(infname) ## ## for line in textfile: ## wordlist = line.split() ## for word in wordlist: ## if word in worddict: ## worddict[word] += 1 ## else: ## worddict[word] = 1 ## textfile.close() ## ## outfile = open(statfile, "w") ## for word in worddict: ## outfile.write(word + ", " + str(worddict[word]) + "\n") ## outfile.close() #### end of textstat #### 字典项排序输出 ## for word in sorted(worddict.keys()): ## outfile.write(word + ", " + str(worddict[word]) + "\n") #### 单词清理,消除无关字符,请自己定义 ## word = clarify(word)