def total(regular, midterm, final):
return regular * 0.2 + midterm * 0.2 + final * 0.6
def main():
with open('score.txt', 'r') as f:
content = f.readlines()
i = 0
for line in content:
line_list = line.strip('\n').split()
content[i] = line_list
t = total(int(content[i][1]), int(content[i][2]), int(content[i][3]))
content[i].append(t)
i = i + 1
content.sort(key=lambda x:x[4], reverse=True)
with open('score_sorted.txt', 'w') as f:
for line_list in content:
print(line_list)
line = ''
for i in range(4):
line = line + line_list[i] + ' '
line = line + '\n'
f.write(line)
main()
import sys
while True:
choose = int(input("请输入您的选择:(1--添加,2--查询,3--退出):"))
if choose == 1: # 添加
f = open("dict.txt", 'a')
str1 = input("请输入要添加的英文单词:")
str2 = input("请给出中文解释:")
line = str1 + "," + str2 + '\n'
f.write(line)
f.close()
elif choose == 2: # 查询
flag = 0
f = open("dict.txt", 'r')
word = input("请输入要查询的单词:")
while True:
s = f.readline()
if not s:
pos = s.find(',')
s1 = s[0: pos]
if word == s1:
print("单词释义:{}".format(s[pos: len(s)]))
flag = 1
break
else:
break
if flag == 0:
print("字典中没有这个单词!")
f.close()
elif choose == 3: # 退出
sys.exit()
else:
print("选择错误,请重输!")
import csv
f = open('record.txt', 'r')
subject=('学号','姓名','语文','数学','英语','信息学','音乐')
with f:
reader = csv.DictReader(f)
for row in reader: # row的类型为: <class 'collections.OrderedDict'>
for i in range(2,7):
if int(row[subject[i]])<60:
print(row[subject[0]],row[subject[1]],subject[i],':',row[subject[i]])
'''
if int(row['语文'])<60:
print(row['学号'],'语文',':',row['语文'])
if int(row['数学'])<60:
print(row['学号'],'数学',':',row['数学'])
'''
1607402002 李子昂 语文 : 50
1607402003 姜涛 信息学 : 55
1607402004 秦学智 英语 : 57
1607402004 秦学智 信息学 : 45
1607402009 祁政 音乐 : 56
1607402012 万震宇 数学 : 59
1607402013 赵轩 信息学 : 57
# 编写程序读文件(内容是“很满意,满意,一般,不满意”组成)然后统计各评语出现的次数,
# 并将最终统计结果追加至“result.txt”文件中
# 编写读文件函数,返回字符串
def readFile(fileName):
file = open(fileName,'r')
res = file.read()
return res
# 统计各评语出现的次数,结果放在字典里。
def countComments(result):
resList = result.split(',')
commentCnts = { }
for res in resList:
commentCnts[res] = commentCnts.get(res,0)+1
return commentCnts
result = readFile("result.txt")
dicCnts = countComments(result)
most = max(dicCnts.values())
for k,v in dicCnts.items():
if v == most:
mostComment = k
break
file = open("result.txt",'a')
file.write("根据统计,对今天伙食感觉:\n")
file.write("'很满意'的学生{}人;\n".format(dicCnts["很满意"]))
file.write("'满意'的学生{}人;\n".format(dicCnts["满意"]))
file.write("'一般'的学生{}人;\n".format(dicCnts["一般"]))
file.write("'不满意'的学生{}人。\n".format(dicCnts["不满意"]))
file.write("出现次数最多的评语是{}\n".format(mostComment))
file.close()
import jieba
import string
with open('苏州园林.txt','r') as f:
text = f.read()
allwords = jieba.lcut(text) # lcut方法返回列表,cut方法返回生成器
counts = { }
for word in allwords:
if word not in '\n\u3000,。“”?!|、 ':
counts[word] = counts.get(word,0) + 1
# 将字典词频从高到低排序,因为字典是无序的,所有采用列表进行
items = list(counts.items())
items.sort(key = lambda x:x[1],reverse = True)
high_frequency = [ ]
for i in range(10):
word,count = items[i]
high_frequency.append(word)
print('{0:<10}{1:>5}'.format(word,count))
的 58
是 16
和 14
苏州园林 11
有 11
园林 11
在 8
游览者 8
而 8
了 8
#本函数将文本全部转换为小写字符,且将非英文字符都转换为空格
import string
def inittxt():
'''
读取英文文本文件,并将全部标点符号替换成空格
'''
with open(r'AesopsFalbes.txt') as f:
text = f.read()
text = text.lower()
for ch in string.punctuation:
text = text.replace(ch,' ')
return text
alltext = inittxt()
allwords = alltext.split() # 根据空格分割字符串,返回英文单词列表
# 根据列表进行统计
counts = { }
for word in allwords:
counts[word]=counts.get(word,0) + 1
# 将字典词频从高到低排序,因为字典是无序的,所有采用列表进行
items = list(counts.items())
items.sort(key = lambda x:x[1],reverse = True)
# 打印并存储频率最高的10个词
high_frequency = []
for i in range(10):
word,count = items[i]
high_frequency.append(word)
print('{0:<10}{1:>5}'.format(word,count))
# 将字典中10个高频词移除
for word in high_frequency:
counts.pop(word)
the 933
and 511
to 387
a 346
he 235
of 210
his 173
that 153
in 140
it 139