FAQ
Hey,

first of all sorry if this has double posted my mail progie did some
funny stuff

I was just creating an encryption/ decryption program in python, ive
got no problem with the algothims or anything like that. my only
difficulty is how to i take a file and read in each charecter and then
preform and operation to it and then output it to another file,
basically i just wanna know how to take a string and then perform
actions to each charecter

cheers

greg

Search Discussions

  • Andreas Kuntzagk at Aug 1, 2003 at 1:59 pm

    I was just creating an encryption/ decryption program in python, ive got
    no problem with the algothims or anything like that. my only difficulty is
    how to i take a file and read in each charecter and then preform and
    operation to it and then output it to another file, basically i just wanna
    know how to take a string and then perform actions to each charecter
    For example like:

    def code_func(c):
    # something clever
    return new_c

    result = "".join(map(code_func,my_string)) #if code(c) returns a string
    or
    result = "".join([str(item) for item in map(code_func,my_string)])

    or you could do:

    resultchars=""
    for c in my_string:
    # some computation
    resultchars.append(result)
    result = "".join(resultchars)

    (Beware: untested code)

    Andreas
  • Mel Wilson at Aug 1, 2003 at 2:19 pm
    In article <cdac0350.0308010032.378d6f08 at posting.google.com>,
    gregadelliot at hotmail.com (jeff) wrote:
    I was just creating an encryption/ decryption program in python, ive
    got no problem with the algothims or anything like that. my only
    difficulty is how to i take a file and read in each charecter and then
    preform and operation to it and then output it to another file,
    basically i just wanna know how to take a string and then perform
    actions to each charecter
    I think (untested code ahead), for small files whose
    contents fit comfortably in memory:

    for character in my_input_file.read():
    my_output_file.write (modified (character))

    For large files, some variant of

    characters = my_input_file.read (1024)
    while characters:
    for char in characters:
    my_output_file.write (modified (char))
    characters = my_input_file.read (1024)

    with the buffer-size changed from 1024 to some better
    number, if necessary.

    Regards. Mel.

Related Discussions

Discussion Navigation
viewthread | post
Discussion Overview
grouppython-list @
categoriespython
postedAug 1, '03 at 8:32a
activeAug 1, '03 at 2:19p
posts3
users3
websitepython.org

People

Translate

site design / logo © 2022 Grokbase