Programming C, C++, Java, PHP, Ruby, Turing, VB
Computer Science Canada 
Programming C, C++, Java, PHP, Ruby, Turing, VB  

Username:   Password: 
 RegisterRegister   
 File character replacement not working.
Index -> Programming, Python -> Python Help
View previous topic Printable versionDownload TopicSubscribe to this topicPrivate MessagesRefresh page View next topic
Author Message
Pockets




PostPosted: Mon Nov 10, 2008 4:52 pm   Post subject: File character replacement not working.

I'm using a simple python method that reads from an input text file, does frequency analysis, and replaces the character with the associated most-frequent character. Not 100% perfect, but it should suit my purposes just fine.

The frequency analysis part works just peachy, as does the transposition of the keys. However, for some reason the replace script is not working. Crypt is printed at the end in its original unaltered state. I'm not sure why. Any input would be greatly appreciated.


code:

import sys, string, operator

file = open(sys.argv[1], 'r')
crypt = file.read()
file.close()
dic = {}
for char in string.uppercase:
        dic[char] = crypt.count(char)
freq = dic.items()
dic = {}
freq.sort(key=operator.itemgetter(1))
dic = {}
dic[freq[0][0]] = 'z'
dic[freq[1][0]] = 'q'
dic[freq[2][0]] = 'j'
dic[freq[3][0]] = 'x'
dic[freq[4][0]] = 'k'
dic[freq[5][0]] = 'v'
dic[freq[6][0]] = 'b'
dic[freq[7][0]] = 'p'
dic[freq[8][0]] = 'g'
dic[freq[9][0]] = 'y'
dic[freq[10][0]] = 'f'
dic[freq[11][0]] = 'w'
dic[freq[12][0]] = 'm'
dic[freq[13][0]] = 'c'
dic[freq[14][0]] = 'u'
dic[freq[15][0]] = 'l'
dic[freq[16][0]] = 'd'
dic[freq[17][0]] = 'r'
dic[freq[18][0]] = 'h'
dic[freq[19][0]] = 's'
dic[freq[20][0]] = 'n'
dic[freq[21][0]] = 'i'
dic[freq[22][0]] = 'o'
dic[freq[23][0]] = 'a'
dic[freq[24][0]] = 't'
dic[freq[25][0]] = 'e'


for key in dic.keys():
        crypt.replace(key, dic[key])

print crypt
Sponsor
Sponsor
Sponsor
sponsor
Pockets




PostPosted: Mon Nov 10, 2008 5:03 pm   Post subject: Re: File character replacement not working.

Nevermind, figured it out. crypt = crypt.replace(etc)

Forgot why I hardly use Python.
Zeroth




PostPosted: Mon Nov 10, 2008 5:20 pm   Post subject: Re: File character replacement not working.

why do you hardly use python?
delparnel




PostPosted: Mon Nov 10, 2008 5:28 pm   Post subject: Re: File character replacement not working.

That doesn't occur to me as the most effective way to setup that dictionary.
Pockets




PostPosted: Tue Nov 11, 2008 12:10 pm   Post subject: Re: File character replacement not working.

delparnel @ Mon Nov 10, 2008 5:28 pm wrote:
That doesn't occur to me as the most effective way to setup that dictionary.


It's not, but the program is quick and dirty and I couldn't be bothered writing a more elegant solution.
Display posts from previous:   
   Index -> Programming, Python -> Python Help
View previous topic Tell A FriendPrintable versionDownload TopicSubscribe to this topicPrivate MessagesRefresh page View next topic

Page 1 of 1  [ 5 Posts ]
Jump to:   


Style:  
Search: