[Tutor] creating files

Liam Clarke cyresse at gmail.com
Sun Nov 21 08:59:28 CET 2004


Hi Jim, 

I'm glad it worked. 

I've forwarded this to the list because someone else usually posts a
more elegant way to do something.

Have fun,

Liam Clarke

On Sun, 21 Nov 2004 00:36:30 -0500, Jim DeCaro <jdecaro2 at comcast.net> wrote:
> THANK YOU IT WORKED!!!!!!!!
> 
> 
> 
> Jim


---------- Forwarded message ----------
From: Jim DeCaro <jdecaro2 at comcast.net>
Date: Sun, 21 Nov 2004 00:24:21 -0500
Subject: RE: [Tutor] creating files
To: Liam Clarke <cyresse at gmail.com>


Thank you for responding so quickly.

Here is what I have so far....

I will try your logic and see what happens.

Thanks, I'll let you know how it works, and if you want to do anything with
this code, feel free.

Jim



-----Original Message-----
From: Liam Clarke [mailto:cyresse at gmail.com]
Sent: Saturday, November 20, 2004 11:36 PM
To: jdecaro2 at comcast.net; Python Tutor
Subject: Re: [Tutor] creating files

Hi Jim,

Are you able to post your code?

So, you've got a loop (this is pseudo-code)

for element in inputString:
      outputLetter = element
      if element in compareToList:
                outputLetter = otherListItem
       print outputLetter

You could just do this -

saveStringList=[]

for element in inputString:
     outputLetter = element
      if element in compareToList:
                outputLetter = otherListItem
       print outputLetter
        saveStringList.append(outputLetter)

saveString="".join(saveStringList)

outputFile=file('encrypted.bit','w')
outputFile.write(saveString)
outputFile.close()

So saveStringList starts as []

and each time through the loop, your output letter is printed to the
screen, and then appended to saveStringList.

So, if the word 'back' became 'head' when encrypted, the prog would
flow like this -

saveStringList would become ['h'] then ['h','e'] and so forth until
your loop finished and it was ['h','e','a','d']

Then saveString="".join(saveStringList) just concentates the list
elements into the string 'head'.

Which you can then write happily to disk.

Hope it helps.

(I use a list to remove the hassle of globals.)

Liam Clarke

On Sat, 20 Nov 2004 23:36:23 -0500, Jim DeCaro <jdecaro2 at comcast.net> wrote:
> I am very new, and am writing a sketchy program to do a simple encryption
> for a school project.  I have created a loop that iterates through letters
> from a read file, and compares them to a list.  When it finds a match, it
> converts the letter to a another letter from a corresponding list (hence
the
> encryption) and prints it to the screen.  The logic works fine, but I
can't
> save the output to a file.  I will not necessarily know what is in the
file
> when it is read, so I can't tell the program to write specific known data
as
> in the tutorials. The output comes from iterations.  How do I make a
> variable that contains all of the letters combined in 1 string that can be
> saved to a file? I can sometimes get it to write 1 of the letters to the
> file.
>
> I have not gotten to functions yet, so I was hoping there would be a
method
> to save each iterated letter to a variable. I have seen the append method,
> but I'm not sure about it.
>
> Thanks,
>
> Jim
>
> _______________________________________________
> Tutor maillist  -  Tutor at python.org
> http://mail.python.org/mailman/listinfo/tutor
>

--
'There is only one basic human right, and that is to do as you damn well
please.
And with it comes the only basic human duty, to take the consequences.




-- 
'There is only one basic human right, and that is to do as you damn well please.
And with it comes the only basic human duty, to take the consequences.
-------------- next part --------------
#Python 2.3 Decryption/Encryption Program
#CSE 428 Progammin Language Concepts
#Fall II, 2004

#James J. DeCaro
#Front Row Plus 1 Group

print "************"
print "*Python 2.3*"
print "************"
print                                                 
print "This program will input data from a file"
print "encrypt it, and save the encrypted data"
print "to a file.  It will also decrypt data"
print "and save the decrypted data to a seperate file."
print                                              
                                                            
encrypt = ['.',',','!',';','1','2','3','4','5','6','7','8','9','0',' ','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','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']
decrypt = ['.',',','!',';','1','2','3','4','5','6','7','8','9','0',' ','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','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','a']

f=open('a:\plain.txt','r')
print f
print                                                   
print "Press ENTER to view the contents of the file."
z=raw_input()
y=f.read()
print y
print     
print "The encrypted form of the data is: "
print


i=0 
while i <(len(y)): 
    c=y[i]
    i=i+1
    j=0
    while j <(len(encrypt)):
        d=encrypt[j]
        if (c == d):
               e = decrypt[j]
        j=j+1
    print(e),

l=str(e)     
g=open('a:\encrypt.txt','w')
h=g.write(l)    
print
print
print "The decrypted form of the data is: "
print
i=0 
while i <(len(y)): 
    c=y[i]
    i=i+1
    j=0
    while j <(len(decrypt)):
        d=encrypt[j]
        if (c == d):
               e = encrypt[j]
        j=j+1

    print(e),
f.close()
g.close()
a=raw_input()                                                                                                        


    
           


 



More information about the Tutor mailing list