Count the number of times a letter appears in a text file in python -


i'm aiming create python script counts number of times each letter appears text file. if text file contained hi there, output

e shown 2 times h shown 2 times shown 1 time r shown 1 time t shown 1 time 

i've tried different ways of getting have no output being shown carry on getting syntax errors. i've tried following

import collections import string  def count_letters(example.txt, case_sensitive=false):     open(example.txt, 'r') f:         original_text = f.read()     if case_sensitive:         alphabet = string.ascii_letters         text = original_text     else:         alphabet = string.ascii_lowercase         text = original_text.lower()     alphabet_set = set(alphabet)         counts = collections.counter(c c in text if c in alphabet_set)      letter in alphabet:          print(letter, counts[letter])     print("total:", sum(counts.values()))      return counts 

and

def count_letters(example.txt, case_sensitive=false):     alphabet = "abcdefghijlkmnopqrstuvxyzabcdefghijklmnopqrstuvxyz"     open(example.txt, 'r') f:         text = f.read()     if not case_sensitive:         alpahbet = alphabet[:26]         text = text.lower()         letter_count = {ltr: 0 ltr in alphabet}     char in text:         if char in alphabet:             letter_count[char] += 1     key in sorted(letter_count):         print(key, letter_count[key])     print("total", sum(letter_count()))  

there few problems found when running script. 1 correctly found @priyansh goel in answer: can't use example.txt parameter. should choose variable name text_file , when call function, pass in string of file's name.

also there indentation error or two. here's script got work:

import collections import string  def count_letters(text_file, case_sensitive=false):     open(text_file, 'r') f:         original_text = f.read()     if case_sensitive:         alphabet = string.ascii_letters         text = original_text     else:         alphabet = string.ascii_lowercase         text = original_text.lower()         alphabet_set = set(alphabet)         counts = collections.counter(c c in text if c in alphabet_set)      letter in alphabet:         print(letter, counts[letter])      print("total:", sum(counts.values()))      return counts  count_letters("example.txt") 

if ever use on "example.txt", rid of first parameter , hard code file name function:

def count_letters(case_sensitive=false):     open("example.txt", 'r') f:         ...  count_letters() 

one of best skills can develop programmer learning read , understand errors thrown. they're not meant scary or frustrating (although are), they're meant helpful. syntax errors had useful. if isn't totally obvious errors indicating, copy , paste error google search , more not you'll find answer question exists out there.

good luck in learning! python great choice (presumably) first language!


Comments

Popular posts from this blog

java - nested exception is org.hibernate.exception.SQLGrammarException: could not extract ResultSet Hibernate+SpringMVC -

sql - Postgresql tables exists, but getting "relation does not exist" when querying -

asp.net mvc - breakpoint on javascript in CSHTML? -