Link to home
Start Free TrialLog in
Avatar of JonKho83
JonKho83

asked on

How do I put the list of values into a variable?

Dear all,

Currently I am facing a problem in putting a list of values from the textfile into a "letter" variable. Here is my coding.

freqfile = open("C:/TextFiles/letFreqM.txt","r")

numlines = 0
for line in open("C:/TextFiles/letFreqM.txt", "r"): numlines += 1

for line in freqfile:
    letter_string,percent_str = line.split()
    percent = float(percent_str) # convert to float
    letter = letter_string[0] # take first (upper case) letter
    print "<%s> <%f>" %(letter,percent)

letters = [ chr(ord('A')+v) for v in range(numlines)]# It will end at the 23th letter
print letters
print letter

Open in new window


So far I can display out the words but I only can put a single word, "Z" into the "letter "variable".

I hope to get it working like this...
['A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', 'K', 'L', 'M', 'N', 'O', 'P', 'R', 'S', 'T', 'U', 'W, 'Y', 'X','Z']
which is found in the text file

I have attached the required files for your viewing.

Thank you.

 files.zip
Avatar of Hoegje
Hoegje

Make it read:
letter = [] # before the for loop

# and inside the loop do:
letter.append(letter_string[0])
Avatar of gelonida
Hi Jon,

I'm not sure, that I understand what you want.
Do you want to read the file letFreq.txt and create a lookup table of which letter corresponds to which letter frequency?

Perhaps you could clarify?
First some comments to your code.


The first line
freqfile = open("FreqEng.txt","r")
opens a file, but then you do nothing with.until line 6
for line in freqfile:


you just open it again a second time with a file handle to count the number of lines.
Why do you want to count the number of lines?
it doesn't have really a meaning if you have letters from 'A' to 'Z' with the missing letters 'Q' , 'V' and 'X'


then at line six you read the file contents again, print out the result, but save nothing

then you create a list of letters from 'A' to 'V'  (same amount of letters as in the original file, but not the same letters)

So how to probably best aproach the problem:

1.) try to read the file only one (no need to count the line up front)
in the loop you can parse the letter and its probability and then do whatever
you like to do with it.

Depending on what your exact inention is do either
2.a) just append each found letter to a list


or

2.b) add the letter and its probability to a dict.


I'll show 2.a)_ first






# it's probably nice to have the filename in a variable.
freq_filename = "letFreqM.txt"

# create the list, that shall contain all the letters, that
# you find find in your ltter frequency table.
letters = [] 

freq_file = open(freq_filename,"r")
for line in freq_file:
    letter_string,percent_str = line.split()
    percent = float(percent_str) # convert to float
    letter = letter_string[0] # take first (upper case) letter
    print "<%s> <%f>" %(letter,percent)
    # just append the letter, that you found to the list of letters.
    letters.append(letter) 
     
print "I found %d letters" % len(letters)
print "and they are:",letters

Open in new window

And I think I would solve it like this:


   

freqfile = open("C:/TextFiles/letFreqM.txt","r")
allLines = freqfile.readlines()
freqfile.close()

numLines = len(allLines)
for aLine in allLines:
    capital = aLine[0]
    letter_string,percent_str = line.split()
    percent = float(percent_str) # convert to float
    print "<%s> <%f>" %(letter,percent)

Open in new window

Now let's assume you want to create a lookuop table with letter frequencies and not only the letter name.
instead of
for letter in letters:
    print "%s : %7.4f" % (letter,freq_table[letter])

you could also have written (if you don't want to keep use the letters variable)
for letter in sorted(freq_table.keys()):
    print "%s : %7.4f" % (letter,freq_table[letter])


Perhaps it's useful to read a little about python's basic data structures and the existin mehtods.
http://docs.python.org/tutorial/datastructures.html#




# it's probably nice to have the filename in a variable.
freq_filename = "letFreqM.txt"

# create the lookup table ( dict ), 
#that shall contain the frequencies for all the letters in your file.
freq_table =  {}  # empty dict is {} / empty list is []

freq_file = open(freq_filename,"r")
for line in freq_file:
    letter_string,percent_str = line.split()
    percent = float(percent_str) # convert to float
    letter = letter_string[0] # take first (upper case) letter
    #print "<%s> <%f>" %(letter,percent)
    # just add letter and percentage to your dict
    freq_table[letter] = percent
     
print "I found %d letters and their probabilities" % len(freq_table)
# get a list of all keys (in your case the letters) of your lookup table
# don't forget, that keys will be returned unsorted
letters_unsorted = freq_table.keys()
print "all the letters (unsorted) are:"
print letters_unsorted

# now get a sorted version and display it
letters = sorted(letters_unsorted)
print "all the letters (sorted) are:"
print letters


#now print the frequency table's contetns
for letter in letters: 
    print "%s : %7.4f" % (letter,freq_table[letter])

Open in new window

Only a minor thing to add, the freq_file should be closed after the for loop:

freq_file.close()
yes, this is true. thanks pepr!
Avatar of JonKho83

ASKER

Hi gelonida and pepr,

Thanks again for your help!! I have complied your work into the python file, gelonid_work.py. It is working great!! But when I try to integrate it into pygenere.py It does not seem to work and kept giving me the following error for past 2hrs:

Traceback (most recent call last):
  File "I:\Mini-Project\testRun.py", line 16, in <module>
    print "Using VigCrack to display keyword: " + VigCrack(ciphertext).crack_codeword()
  File "I:\Mini-Project\pygeneremod_Backup.py", line 408, in crack_codeword
    break
  File "I:\Mini-Project\pygeneremod_Backup.py", line 347, in __find_codeword
    # Find out which value of kappa is closest to the kappa of the
AttributeError: 'VigCrack' object has no attribute 'set_language'

I am not sure where I have went wrong... I can feel the "answer" is very near.. But I am unable to reach it and I hope I can with all of your help... :(

Just note that...
gelonid_work.py solves the above problem.. but I have problem trying to integrate to pygeneremod_Backup.py

TestRun.py file is the "controller" for me to test the decryption and need to change the import values from pygeneremod_Backup and pygeneremod..

pygeneremod.py is working fine for decrypting english by using TestRun.py and the text files on letFreqEnglish and  ciphertextEng to decrypt it.

I am working on pygeneremod_Backup.py which gives me the above error..

I would appreicate it if anyone can help me to take a look on it..

Thank you for your time reading this post.
Mini-Project.zip
Jon,


Sommegeneral  recommendations which might allow more efficient debugging and communication:


1.) look for a good diff tool
--------------------------------
I would strongly recommend you to use a program, which helps you looking at differences of two files.

Under Windows you could for example use Beyondcompare , though I don't know this tool personally as I'm working mostly under linux.

2.) change as little as possible in order to be focused on differences, that really matter
-----------------------------------------------------------------------------------------------------------

Then I recommend to change as little as possible between both files,such that you can
quicky look for the reall differences.


3.) do not use absolute path names in your examples.
----------------------------------------------------------------------
if you want others to test your code, then I strongly recommend to NOT use
absolute path names like 'c:/TextFiles/ciphertextEng.txt'

Just use 'TextFiles/ciphertextEng.txt',

then others can try your code immediately without having to chaneg the file names.










The probblem is, that one weird change slipped in your code,


line 365 of pygeneremod_Backup.py\

you have

        alpha_only = VigCrack(''.join(accumulator)).set_language(self.__lang_data

in the original code this was
        alpha_only = VigCrack(''.join(accumulator)).set_language(self.__lang)


The next thing to consider is of course.

If you removed all tables for other languages, then the function set_language is rather useless.

hi gelonida,

Thank for your tips.. Will look into it right away.
The AttributeError: 'VigCrack' object has no attribute 'set_language' says that you did not define the method and you want to call it or you did not define anything else of that name and you want to use it.

After unzipping your Mini-Project.zip, after slight modification of hardwired paths, and after running "python testRun.py" I could not reproduce your error.  Instead, I have got another one -- see below.  What version of Python do you use?

C:\tmp\___python\JonKho83\Q_26328009\Miniproject>python testRun.py
I found 26 letters and their probabilities
all the letters (sorted) are:
['A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', 'K', 'L', 'M', 'N', 'O', 'P',
 'Q', 'R', 'S', 'T', 'U', 'V', 'W', 'X', 'Y', 'Z']
0.0650292191096
12.702
A :  8.1670
B :  1.4920
C :  2.7820
D :  4.2530
E : 12.7020
F :  2.2280
G :  2.0150
H :  6.0940
I :  6.9660
J :  0.1530
K :  0.7720
L :  4.0250
M :  2.4060
N :  6.7490
O :  7.5070
P :  1.9290
Q :  0.0950
R :  5.9870
S :  6.3270
T :  9.0560
U :  2.7580
V :  0.9780
W :  2.3600
X :  0.1500
Y :  1.9740
Z :  0.0740
mahsg trdev qgtzh stkha ryqsg flgij wurya skmft qsgmh eqjtx rgbgk usmax hdlaa db
zok bgfmw gvjrm xjqgh rgszv bzeda smdmg bghaj avkbs ranav mrlxg euxak atzwt wmzt
x svvim mvngk ncsza vtefl uhdvm xltzk dwutz evqsg nefsa gsggz hhdel qggur xwjfg
wstdh rlgeg ndggc fmdcf wokuw cavru rywxl fejds musfi jnysm zlamx kuqbz mlqse fl
gcy afekq kpuxu pgyrq qzswu sszmf ayjwj iawkq tekez wsqbv gxszm rviaf gvecj tzba
w mlxcs alaay ypbne xtgkt kmfok huqao zmmpd iqfkh gctme xqxav mphwd bfmuq sgetj
whtse flsug xhcte uggxa gusfo jtpys tdrmf kwjue mlikt reuba gpmnq vnmpk altdx kb
jnf xavyt dsmvv puxza lhgxu tmuej gwvzx sjqgn ejgwv beeuw efaru zqhog saahz edtq
l grybg umvla enemu eamts jlagm vqpsk pagzc bzkxw ddtej iklur ywxyu gaidl nslpd
hoedi fwzel qggmp svvvg vkqfz oehql txmkb ktzhu ihmmm fagyu rvckm dcuwh leljq fd
rvg uyhur ywmma rfqya fwzqx murei uhzee wtlfs jukty yfqub bedjm bxhav ybzvg kswm
q gcjkg jxqsg evgjs amryi jygpd wxyas valnr jkxae xeyqf zfsfa gyfyf ixbel eqddm
rvkgv aesas eessd slfum ifmux qwxva vfide ajabs emwlw ltxpq cfyux xwjag qsvuh zw
muh musfe amtsm bxnew gzswa eflft fyjid ekaab zhgxs vqlav lwxmd mstsg urvqy gmrl
k jhihz ifzew gtavu xgzkh zezik mupqj mbxxk ksyrs dlagr vgvlh rrsvu repse uhgvl
alauw fifvk mkilh irfwl tisei ftzhj iflmg casea gstbh gvfid tpmko mlfmf ojbsl le
ags vsola mxaak bpmfo szmmf albfy hifwp sovla uwdif wajgc jlxsg bagsl satka yypl
w agcak aatki fwrej ukmae nqjmg edalt zhkba expsl albej qlbar tgyxa vymki qvwkl
kmrkt smqht gybxf wzltp eazsl mzgqv
Traceback (most recent call last):
  File "testRun.py", line 12, in <module>
    print "Using VigCrack to display keyword: " + VigCrack(ciphertext).crack_cod
eword()
  File "C:\tmp\___python\JonKho83\Q_26328009\Miniproject\pygeneremod_Backup.py",
 line 426, in crack_codeword
    return self.__find_codeword(array[0], array[1])
  File "C:\tmp\___python\JonKho83\Q_26328009\Miniproject\pygeneremod_Backup.py",
 line 365, in __find_codeword
    alpha_only = VigCrack(''.join(accumulator)).set_language(self.__lang_data)
  File "C:\tmp\___python\JonKho83\Q_26328009\Miniproject\pygeneremod_Backup.py",
 line 259, in set_language
    self.__lang = language.upper()
AttributeError: 'dict' object has no attribute 'upper'

C:\tmp\___python\JonKho83\Q_26328009\Miniproject>

Open in new window

I woud suggest some restructuring of your code:


create one file, that cares only for generation of the table information

and the other with your crypto libraries.


Mixnig table gneration and the crypto algorithm is in my opinion not a godd idea and
makes it more difficult to debug.


Please see attached file.


restructured.zip
Hi gelonida,

Now I am using Beyondcompare tool to help me on my programming issue.
That is one of the great tip given to me and I just feel that it's kinda of pity I didn't got the chance to discover it earlier and save my time.

Thanks man.

Now I am looking to your restructure codes and looking at the programming concept behind it.

Hi pepr,

I am using Python 2.6.4 32bit version.

Dear all,
I am really thankfully for you guys being patience with me as I am still new to python(started using it four months ago and currently I am learning C#).

Now I will be trying out all the feedbacks given to me and will be back soon.

Thanks.
hi gelonida,

did your console printed this out?
"Using VigCrack to display keyword: TMENISTMESIS"

The keyword is supposed to be "tmesis" and I tried deleting .pyc files.. It still gave me this keyword..

Now I am looking into the requirements into switching between 2 different languages.

Thanks.
to all,

i changed line 105 of language_tables.py file..
from
freq_table['kappa'] = kappa
to this...
freq_table['kappa'] = kappa + 0.001670780890445228

Not too sure why...

Now I am looking to remove all static data and using the the data from the text file at one time only..
Dear all,

In order to change the language, go under __lang = 'MY' found under pygeneremod.py file, line 200
 or you can use the search function using this keyword, ---> #changelang

It seems that I can get the keyword correctly for decrypting another unknown language.
The print msg on the console. "Using VigCrack to display keyword: KETIBAAN"
But I can't seem to get the message decrypted when being compared to "plaintextM1" text file..

I am thinking that line pygeneremod_Backup.py, line 315.
"codeword.append(chr(ord('A') + (26 - shift)))"

Cause the decryption of the message not successful.

Thanks.

I have attached the files for your viewing.

Note: Use use testRun.py as a controller.

Thanks
expertexchangeinfo.zip
Dear all,

I tried to change the initial value of 26 to 23.. yet there is no improvement on the case..

Any comments on how should I approach this problem would be great!!

Thanks!!
Hi Jon,

sorry I''m currently very busy.

I will look at it either saturday or next week.

I can suggest only twothings meanwhile

1.) Try to use a much longer text.

2,) Try to calculate the letter frequency distributon, of your original text and look if it comes close to your table values
John,

Apologies I'm still very busy and don't have a lot of time in the next weeks.
I'd suggest you close this question.

I think the original answer has been answered (putting list of values in variables, or better
reading file contents and stor it in a dict)

You could try to create a new question describing exactly the problem, that you're currently having.
I'd also suggest to try to make your code more modular (as I suggested in 33203730 )
It will make it easier for others to help.

The problems, that you're having now might be more of algorithmic nature than of python nature.
(perhaps there's an issue with the calculation of kappa)

With the current title and zones chances are low, that you get any help from algorithmic persons.

Ideally any new question could perhaps have as zones
python and Math/Science
hi man, thanks for the effort. I think I would just close the question as it is getting abit too complex for me.. will take more time to resolve it..
ASKER CERTIFIED SOLUTION
Avatar of gelonida
gelonida
Flag of France image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
I agree with gelonida.  There are some rules here.
Dear all,

Just to update you all on my case. For the past week, I have been working on similar program using VB 2008 with two of my friends' help and their constant encouragement. Thankfully with their help I am able to "see" it happen(Able to decrypt any languages given which is far from what I have).

Now on this question's issue, I apologize if I have offended anyone and I assure you that it was unintentionally.

After reading the info on EE's rules on "Grades". I would know what to do.

Thanks.

Regards,
JonKho
Thanks for the previous help!