[Tutor] creating files
Liam Clarke
cyresse at gmail.com
Sun Nov 21 05:36:21 CET 2004
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.
More information about the Tutor
mailing list