String Manipulation

Discussion in 'Python' started by Michael Jordan, Jul 13, 2005.

  1. i'll be straight with you and say that this is a homework assignment.
    ive tried to figure it out on my own but am now out of time.

    i need to go through a .txt file and get rid of all punctuation. also,
    every time i see the work "Fruitloops=1" or "Hamburgers=x" where x is
    ANY number i need to get rid of that also. thanks a bunch. hurry
    please!

    jen :)
     
    Michael Jordan, Jul 13, 2005
    #1
    1. Advertising

  2. Michael Jordan

    Larry Bates Guest

    Use .replace function to replace punctuation (you didn't say
    exactly what that means but just to get you started):

    #
    # Extend this list as needed
    #
    punctuations=',.;:()'
    #
    # Open input and output files
    #
    ifp=open(inputfilename,'r')
    ofp=open(outputfilename,'w')
    #
    # Strip out the punctuation characters
    #
    for line in ifp:
    for punctuation in punctuations:
    line=line.replace(punctuation,'')
    ofp.write(line)

    #
    # I'll leave the other part for homework but
    # you will need to use the .find method of the string
    #

    ifp.close()
    ofp.close()

    Larry Bates


    Michael Jordan wrote:
    > i'll be straight with you and say that this is a homework assignment.
    > ive tried to figure it out on my own but am now out of time.
    >
    > i need to go through a .txt file and get rid of all punctuation. also,
    > every time i see the work "Fruitloops=1" or "Hamburgers=x" where x is
    > ANY number i need to get rid of that also. thanks a bunch. hurry
    > please!
    >
    > jen :)
    >
     
    Larry Bates, Jul 13, 2005
    #2
    1. Advertising

  3. Michael Jordan

    Josef Meile Guest

    Hi,

    > for punctuation in punctuations:
    > line=line.replace(punctuation,'')

    I would use maketrans or even a regex instead. However, If you care
    about speed, it is well known that in some cases regex take more
    time than multiple replaces. Even the maketrans could take more time
    (I don't know; you may benchmark it -> homework)

    Regards,
    Josef
     
    Josef Meile, Jul 13, 2005
    #3
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. jared in ecs

    Perl string manipulation

    jared in ecs, Oct 22, 2003, in forum: Perl
    Replies:
    2
    Views:
    823
    Roy Johnson
    Oct 22, 2003
  2. Garfield

    String Array Manipulation Problem

    Garfield, Aug 27, 2003, in forum: ASP .Net
    Replies:
    5
    Views:
    2,837
    Michael Lang
    Aug 27, 2003
  3. Aaron

    String Manipulation

    Aaron, Dec 31, 2003, in forum: ASP .Net
    Replies:
    3
    Views:
    4,664
    Dave Davidson
    Dec 31, 2003
  4. Aaron

    Advanced String Manipulation (C#)

    Aaron, Jan 2, 2004, in forum: ASP .Net
    Replies:
    2
    Views:
    860
  5. Chris

    String manipulation

    Chris, May 24, 2004, in forum: ASP .Net
    Replies:
    1
    Views:
    466
    Pete Wright
    May 24, 2004
Loading...

Share This Page