1.
# -*- coding:utf-8 -*- filename = raw_input(u"请输入文件名: ".encode('gbk')) fobj = open(filename) for eachline in fobj: if eachline.startswith('#'): continue elif '#' in eachline: print eachline[:eachline.index('#')] else: print eachline,
2.
# -*- coding:utf-8 -*- filename = raw_input(u"请输入要打开的文件名".encode('gbk')) try: fobj = open(filename) except: print u"文件不存在,请验证!!" lines = int(raw_input(u"请输入一个数字,以显示文件的前若干行".encode('gbk'))) filecontent = fobj.readlines() filelines = len(filecontent) if lines <= filelines: for eachline in filecontent[:lines]: print eachline, else: print u"需打印内容超出文件大小!该文件只有%d行,下面将打印该文件全部内容!" % len(filecontent) for eachline in filecontent: print eachline,
3.
# -*- coding:utf-8 -*- filename = raw_input(u"请输入要打开的文件名".encode('gbk')) try: fobj = open(filename) except: print u"文件不存在,请验证!!" filelines = len(fobj.readlines()) print u"文件共有 %d 行!" % filelines
4.
# -*- coding:utf-8 -*- filename = raw_input(u"请输入要打开的文件名".encode('gbk')) try: fobj = open(filename) except: print u"文件不存在,请验证!!" filecontent = fobj.readlines() filelines = len(filecontent) print u"文件共有 %d 行!" % filelines i = 1 while i <= filelines: print filecontent[i-1], if i % 25 == 0: prompt = u"已经打印 %d 行,按任意键继续!" % i raw_input(prompt.encode('gbk')) i += 1 continue i += 1 print u"文件打印完毕!"
5.
# -*- coding:utf-8 -*- # 本例中,学生的成绩存放于Excel表格中,其中第一列为姓名、第二列为成绩 from __future__ import division import xlrd filename = raw_input(u"请输入存放成绩的表格文件名".encode('gbk')) data = xlrd.open_workbook(filename) table = data.sheets()[0] name = table.col_values(0)[1:] score = table.col_values(1)[1:] report = dict(zip(name, score)) i = 0 sum = 0 for name in sorted(report): i += 1 eachscore = report[name] sum = sum + eachscore if "90" <= eachscore <= "100": print u'第 %d 个学生是%s, 你的分数是 %s, 评分是 "A"' % (i, name, eachscore) elif "80" <= eachscore <= "89": print u'第 %d 个学生是%s, 你的分数是 %s, 评分是 "B"' % (i, name, eachscore) elif "70" <= eachscore <= "79": print u'第 %d 个学生是%s, 你的分数是 %s, 评分是 "C"' % (i, name, eachscore) elif "60" <= eachscore <= "69": print u'第 %d 个学生是%s, 你的分数是 %s, 评分是 "D"' % (i, name, eachscore) else: print u'第 %d 个学生是%s, 你的分数是 %s, 评分是 "E"' % (i, name, eachscore) average = sum / i print u"所有学生的平均分是: %.2f" % average
6.
# -*- coding:utf-8 -*- import sys filename1 = raw_input(u"请输入第一个待检查的文件名".encode('gbk'))[1:-1] filename2 = raw_input(u"请输入第二个待检查的文件名".encode('gbk'))[1:-1] fobj1 = open(filename1) fobj2 = open(filename2) file1 = fobj1.readlines() file2 = fobj2.readlines() dif_row = 0 for i in range(min(len(file1),len(file2))): dif_row += 1 if file1[i] == file2[i]: continue else: for j in range(min(len(file1[i]),len(file2[i]))): if file1[i][j] == file2[i][j]: continue else: dif_column = j + 1 break else: dif_column = min(len(file1[i]),len(file2[i])) + 1 break else: if len(file1) == len(file2): print u"两个文件完全相同!" sys.exit() else: dif_row = min(len(file1),len(file2)) + 1 dif_column = 1 print u"两个文件在第 %d 行、第 %d 列处不一样。" % (dif_row,dif_column)
7.未懂题意,存疑
8.
# -*- coding:utf-8 -*- modulename = raw_input(u"请输入模块名字:".encode('gbk')) try: module = __import__(modulename) except: print u"输入的模块不存在!" module_element = dir(module) for eachelement in module_element: print u"名字:", eachelement print u"类型:", type(getattr(module, eachelement)) print u'值:', getattr(module, eachelement), '\n'
9-13 待补充
14.
# -*- coding:utf-8 -*- import sys import os def judge_int(num): if "." in num: return float(num) else: return int(num) def calculator(opera, num1, num2): num1 = judge_int(num1) num2 = judge_int(num2) if opera == "+": return num1 + num2 elif opera == "-": return num1 - num2 elif opera == "**" or opera == "^": return num1 ** num2 elif opera == "*": return num1 * num2 elif opera == "/": return num1 / num2 elif opera == "%": return num1 % num2 else: return u"无法识别,请正确输入" if len(sys.argv) == 4: script, calnum1, calopera, calnum2 = sys.argv result = calculator(calopera, calnum1, calnum2) print result memory_content = sys.argv[1:] + ['/n%d\n' % result] memory = open(r"E:\Code\CPP\9\test\9-14.txt",'a') memory.write(calnum1 + calopera + calnum2 + '\r\n') memory.write(str(result) + '\r\n') memory.close() else: if len(sys.argv) == 2: memory = open(r"E:\Code\CPP\9\test\9-14.txt", 'r+') print memory.read() memory.seek(0) memory.truncate() memory.close()
15.
# -*- coding:utf-8 -*- import shutil import sys if len(sys.argv) == 3: old = sys.argv[1] new = sys.argv[2] shutil.copyfile(lod, new) sys.exit() old = raw_input(u"请输入旧文件".encode('gbk'))[1:-1] new = raw_input(u"请输入新文件名".encode('gbk'))[1:-1] newfile = open(r'%s' % new, 'w') newfile.close() shutil.copyfile(r'%s' % old, r'%s' % new) print u"完成!任意键退出!"
16.
# -*- coding:utf-8 -*- filename = raw_input(u"请输入文件名字".encode('gbk'))[1:-1] fobj = open(filename, 'r') oldfile = fobj.readlines() fobj.close() newfile = [] for index, eachline in enumerate(oldfile): if len(eachline) <= 80: newfile.append(eachline) else: lastwordindex = eachline[:80].rindex(" ") newfile.append(eachline[:lastwordindex]) oldfile.insert(index+1, eachline[lastwordindex+1:]) fobj = open(filename, 'w') fobj.writelines(newfile) fobj.close() print u"完成,任意键退出!