file_handle = open('a.txt', mode='w')
file_handle.writelines("You need to have a Kubernetes cluster, and the kubectl command-line tool must be configured \n"
"to communicate with your cluster. If you do not already have a cluster, you can create one \n"
"by using minikube or you can use one of these Kubernetes playgrounds \n")
file_handle.writelines("The preferred way to create a replicated application is to use a Deployment, which in turn \n"
"uses a ReplicaSet. Before the Deployment and ReplicaSet were added to Kubernetes, replicated \n"
"applications were configured using a ReplicationController \n")
file_handle.close()
统计文本中每个单词出现的频率并随机输出6个单词
import random
file_handle = open("a.txt", mode="r")
lines = file_handle.readlines()
file_handle.close()
dict = {}
set = set()
for line in lines:
words = line.split(" ")
for word in words:
if word in dict.keys():
dict[word] += 1
else:
dict[word] = 1
set.add(word)
print(dict)
print(random.sample(list(set), 6))
读出文件内容修改后再写回去
lines = read_handle.readlines()
read_handle.close()
for line in lines:
line = line.replace("<font face=\"宋体\"><font size=\"5\">", " ")
line = line.replace("</font></font>", " ")
# 打开方式分为r、w、a三种方式 ,【+】表示可以同时读写,这里没有使用同时读写,写的open必须放在读关闭之后不然会变成空文件
write_handle = open("a.txt", mode="w")
write_handle.writelines(lines)
write_handle.close()