程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
您现在的位置: 程式師世界 >> 編程語言 >  >> 更多編程語言 >> Python

Problems in reading and using Python files

編輯:Python
“”“ This is a read file , The code of the frequency of the same letter in the statistical file , Then output on the screen , Although it can count the frequency of the same characters , But the upper and lower case systems still think that 2 Letters , Who can give us some advice , And the best use lambda Function to express the frequency of the same character ”“”from os import strerrorcounters = {chr(ch): 0 for ch in range(ord('a'), ord('z') + 1)}print(counters)file_name = input("Enter the name of the file to analyze: ")try: f = open("textfile.txt", "rt") for line in f: for char in line: if char.isalpha(): char.lower() x=set(line) f.close() for item in x: counters[item]=line.count(item.lower()) cnt=counters[item] if cnt > 0: print(item, '->',cnt)except IOError as e: print("I/O error occurred: ", strerror(e.errno))"""output: Input cbaBaa Output a -> 3c -> 1b -> 2

  1. 上一篇文章:
  2. 下一篇文章:
Copyright © 程式師世界 All Rights Reserved