Hello all;
I'm working huffman coding of any .txt file, so first I need to analyse this text file. I need to read it, then analyse.
I need "exit" like table:
****************************
letter frequency(how many times same latter repeated) Huffman code(this will come later)
************************
I started with: -
f = open('test.txt', 'r') #open test.tx
-
for lines in f:
-
print lines #to ensure if all work...
-
Can anyone help me?
4 3041 -
def countLetters(line, letter):
-
ret = 0
-
for character in line:
-
if character == letter: ret += 1
-
return ret
-
-
for line in open("file.txt"):
-
line = line.strip()
-
print line
-
print "Has", countLetters(line, "a"), "of letter a."
-
print "Has", countLetters(line, "e"), "of letter e."
-
print "Has", countLetters(line, "i"), "of letter i."
-
print "Has", countLetters(line, "o"), "of letter o."
-
print "Has", countLetters(line, "u"), "of letter u."
-
print "Has", countLetters(line, "y"), "of letter y."
-
print
Modifying Sean's Code: - #!/bin/python3
-
-
def countLetters(line, letter):
-
ret = 0
-
for character in line:
-
if character == letter: ret += 1
-
return ret
-
-
alphabet = 'abcdefghijklmnopqrstuvwxyz'
-
#you can automate this using ascii codes too
-
-
for line in open("file.txt",'r'):
-
line = line.strip() #remove trailing spaces
-
print (line)
-
for letter in alphabet:
-
print ('Has {0} of letter {1}'.\
-
format(str(countLetters(line,letter)),letter))
output:
gbfchjwshfcjkwhndfnxh;iquw;qemiziqmeuzngyegbfyewgy bqgzeqzydglndhlqhd;jkjnenjcejnrcjercbvvbdbggngnjmm nmsnvsdmsnfsfcsf>mNmvbmsdbfluaheregnctfbaxzmasqojm wqi;htrugttgp
Has 3 of letter a
Has 9 of letter b
Has 7 of letter c
Has 7 of letter d
Has 10 of letter e
Has 9 of letter f
Has 12 of letter g
Has 8 of letter h
Has 4 of letter i
Has 9 of letter j
Has 2 of letter k
Has 3 of letter l
Has 11 of letter m
Has 13 of letter n
Has 1 of letter o
Has 1 of letter p
Has 8 of letter q
Has 4 of letter r
Has 8 of letter s
Has 4 of letter t
Has 4 of letter u
Has 4 of letter v
Has 5 of letter w
Has 2 of letter x
Has 4 of letter y
Has 5 of letter z
-
#-------------------------------------------------#
-
# Set Variables #
-
#-------------------------------------------------#
-
-
input = open("file.txt")
-
whitelist = ('a','b','c','d','e','f','g') # whitelist of letters
-
letters = {}
-
-
#-------------------------------------------------#
-
# Functions #
-
#-------------------------------------------------#
-
-
def count_letter(c):
-
if c in letters:
-
letters[c] += 1 # if letter in letters add one
-
else:
-
letters[c] = 1 # if letter not in letters set add letter to dictionary object
-
-
-
def print_letters(letters):
-
-
for k,v in letters.items():
-
if k in whitelist:
-
print "Has %s of letter %s" % (v,k) # print out count for each letter
-
-
-
#-------------------------------------------------#
-
# Run code #
-
#-------------------------------------------------#
-
-
-
for line in input: # for each line in input file
-
for letter in line: # for each letter in line
-
count_letter(letter) # tally a count of each letter
-
-
print_letters(letters)
-
Here I use a more pythonic syntax, which means less lines of code. If you count everything and whitelist the characters your concerned with then your code can be easily modified in the future.
Hope this helps!
Sign in to post your reply or Sign up for a free account.
Similar topics
by: Abby |
last post by:
My .dat file will contain information like below.
///////////
First
0x04
0x05
0x06
Second
0x07
|
by: py |
last post by:
i need to read the contents of a file (could be 100kb, could be 500mb,
could be 1gb)...but I want to allow the read to be canceled.
for example, say a user wants to savea file...as it's saving...
|
by: pycircle |
last post by:
From a python module, I want to run any.py module, to open it in the shell, just like if I were in IDLE editor and selecting EDIT / Run Module.
I have tried a lot of things, including importing a...
|
by: somequestion |
last post by:
During copying file , wanna read file Size like this
string CheckFileSize(string fileName)
{
if( fileName == null ) return;
FileInfo fi = new FileInfo(fileName);
return fi.Length.ToString();...
|
by: potluri040 |
last post by:
hi,
could any one let me know how to read file thru VB script.
Scenerio is like this:
i am keeping a personal details such as name, age, sex, company, location in a text file called user details...
|
by: yinglcs |
last post by:
How to modify the source of a python file inside a python egg file?
I can see the file by unzipping it, but how can I package it back as a
python egg file after my modification.
Thank you.
|
by: =?Utf-8?B?Sm9obiBXYWxrZXI=?= |
last post by:
Hi,
Is there anything wrong with the code below in sending my browser page to
Excel? Before my page opens in Excel there's a message "Problems came up in
the following areas during load:" and it...
|
by: yinglcs |
last post by:
Hi,
I am trying to use python for file processing.
Suppose I have a file like this:
I want to build a Hashmap between the line "begin_QOS_statistics" and
"end_QOS_statistics"
and for each line...
|
by: xplode144 |
last post by:
I have a Web application. i need to read a file once during the startup and
preserve the read data throughout the life of the application. i will to
access the data often during the page_load of...
|
by: danimian |
last post by:
Hello,
first i am creating xml file if file does not exist.
String myFile = "C:\myxmlfile.xml";
if (!File.Exists(myFile)) {
using (FileStream conStream = new FileStream(myFile,...
|
by: ryjfgjl |
last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
|
by: emmanuelkatto |
last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud.
Please let me know.
Thanks!
Emmanuel
|
by: BarryA |
last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
|
by: nemocccc |
last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
|
by: Sonnysonu |
last post by:
This is the data of csv file
1 2 3
1 2 3
1 2 3
1 2 3
2 3
2 3
3
the lengths should be different i have to store the data by column-wise with in the specific length.
suppose the i have to...
|
by: Hystou |
last post by:
There are some requirements for setting up RAID:
1. The motherboard and BIOS support RAID configuration.
2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
|
by: Hystou |
last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
|
by: Oralloy |
last post by:
Hello folks,
I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>".
The problem is that using the GNU compilers,...
|
by: Hystou |
last post by:
Overview:
Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
| |