首页 > 技术文章 > Python(文件操作实例)

yunlongaimeng 2018-04-05 18:51 原文

 给定一个文件;以及给定的字符,比如“a”; 统计字符个数;(可选)

# 文件的打开操作
f = open("wyl.txt","r")
# 文件的读取操作
content = f.read()
# 用户输入需要统计的字符:
receiveData = input("请输入需要统计的字符:")
count = 0
for temp in content:
if temp == receiveData:
count = count+1
# 输出统计的字符
print(count)


# 文件的关闭操作
f.close()
实验结果:

 


 

 


推荐阅读