Python Forum
get two characters, count and print from a .txt file
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
get two characters, count and print from a .txt file
#1
Hi all, I'm back anyway

The program I wrote is terrible so if anyone can make a different py and give me some pointers well thanks a bunch Wall

What I would like help on is to print only the first two characters of a word and if those same characters repeat hold them to a count. It's like a Zipf distribution but for the first two letters for every word. The example is how I would like the output. Here is an example from this text and its for show!

text file = "Here is an example from this text and its for show!"

an 2
He 1
is 1
ex 1
fr 1
th 1
te 1
it 1
fo 1
sh 1
total 11

file = open("C:\python37\paradise.txt", 'r') 
  
while 1: 
      
    # read by character 
    char = file.read(2)
    if not char:  
        break
          
    print(char) 
  
file.close() 
Reply


Messages In This Thread
get two characters, count and print from a .txt file - by Pleiades - Oct-03-2020, 05:10 AM

Possibly Related Threads…
Thread Author Replies Views Last Post
  Row Count and coloumn count Yegor123 4 1,321 Oct-18-2022, 03:52 AM
Last Post: Yegor123
  Saving the print result in a text file Calli 8 1,784 Sep-25-2022, 06:38 PM
Last Post: snippsat
  failing to print not matched lines from second file tester_V 14 6,072 Apr-05-2022, 11:56 AM
Last Post: codinglearner
  Print to a New Line when Appending File DaveG 0 1,217 Mar-30-2022, 04:14 AM
Last Post: DaveG
Sad Want to Save Print output in csv file Rasedul 5 10,910 Jan-11-2022, 07:04 PM
Last Post: snippsat
  Convert legacy print file to XLSX file davidm 1 1,801 Oct-17-2021, 05:08 AM
Last Post: davidm
  Why it does not print(file.read()) Rejaul84 1 2,332 Jul-01-2021, 10:37 PM
Last Post: bowlofred
  all i want to do is count the lines in each file Skaperen 13 4,817 May-23-2021, 11:24 PM
Last Post: Skaperen
  Split Characters As Lines in File quest_ 3 2,509 Dec-28-2020, 09:31 AM
Last Post: quest_
  How to use the count function from an Excel file using Python? jpy 2 4,434 Dec-21-2020, 12:30 AM
Last Post: jpy

Forum Jump:

User Panel Messages

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