Python Forum
[Help] How to count Letter frequency in a text file?
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
[Help] How to count Letter frequency in a text file?
#4
@ichabod801 and @buran - thank you once again for taking the time sharing your ideas!

I managed to restructure my code and it works fine but my next question is how can I make the output like a table as shown in the screenshot here?

[Image: 070818_zpshfys00vk.png]

==============================================================================

file = open("alice_in_wonderland.txt", "r", errors ='ignore') # open file
charcount = {} #dictionary to hold char counts
validchars = "abcdefghijklmnopqrstuvwxyz" # only these counted

print(": Letter : Frequency :")

for i in range(97,123): # lowercase range
    c = (chr(i)) # the chars a-z
    charcount[c] = 0 # initialize count

for line in file:
    words = line.split(" ") # line into words
    for word in words:  # words into chars
      chars = list(word) #convert word into a char list
      for c in chars:  # process chars
          if c.isalpha():  # only alpha allowd
              if c.isupper():
                  c = c.lower()  # if char is upper convert to lower
              if c in validchars: # if in valid char set
                  charcount[c] += 1 # increment count
                  
print(charcount) # print list

file.close() # close file
Thank you in advance!
Blockchain Visionary & Aspiring Encipher/Software Developer
me = {'Python Learner' : 'Beginner\'s Level'}
http://bit.ly/JoinMeOnYouTube
Reply


Messages In This Thread
RE: [Help] How to count Letter frequency in a text file? - by vanicci - Aug-07-2018, 09:40 PM

Possibly Related Threads…
Thread Author Replies Views Last Post
Thumbs Up Need to compare the Excel file name with a directory text file. veeran1991 1 2,104 Dec-15-2022, 04:32 PM
Last Post: Larz60+
  Row Count and coloumn count Yegor123 4 2,926 Oct-18-2022, 03:52 AM
Last Post: Yegor123
  FFT - frequency shifted frohr 0 1,177 Jul-23-2022, 05:17 PM
Last Post: frohr
  Modify values in XML file by data from text file (without parsing) Paqqno 2 3,266 Apr-13-2022, 06:02 AM
Last Post: Paqqno
  Converted Pipe Delimited text file to CSV file atomxkai 4 11,284 Feb-11-2022, 12:38 AM
Last Post: atomxkai
  all i want to do is count the lines in each file Skaperen 13 7,411 May-23-2021, 11:24 PM
Last Post: Skaperen
  [split] How to convert the CSV text file into a txt file Pinto94 5 4,934 Dec-23-2020, 08:04 AM
Last Post: ndc85430
  How to use the count function from an Excel file using Python? jpy 2 6,175 Dec-21-2020, 12:30 AM
Last Post: jpy
  Saving text file with a click: valueerror i/o operation on closed file vizier87 5 7,287 Nov-16-2020, 07:56 AM
Last Post: Gribouillis
  get two characters, count and print from a .txt file Pleiades 9 5,196 Oct-05-2020, 09:22 AM
Last Post: perfringo

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020