String Manipulation

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

  1. hey, i have this huge text file and i need to go through and remove all
    punctuation and every instance of the phrase "fruitloops=$" where $ is
    any number 0-100" um, and yeah this is homework but i've tried to no
    avail. thanks guys. cheerio :). jen
     
    Michael Jordan, Jul 13, 2005
    #1
    1. Advertising

  2. Michael Jordan

    Bill Mill Guest

    On 13 Jul 2005 07:49:02 -0700, Michael Jordan <> wrote:
    > hey, i have this huge text file and i need to go through and remove all
    > punctuation and every instance of the phrase "fruitloops=$" where $ is
    > any number 0-100" um, and yeah this is homework but i've tried to no
    > avail. thanks guys. cheerio :). jen


    Jen,

    This program iterates through one file and outputs all lines to
    another file which have the word "homework" in them.

    #-------------------------- Begin program 1
    file_in = file('data.in')
    file_out = file('data.out')

    for line in file_in:
    #line is a string containing one line of the file
    if "homework" in line:
    file_out.write("homework")
    #--------------------------- End program 1

    Here is a program which turns a string containing the phrase
    "number=42" into a variable containing the integer 42:

    #-------------------------- Begin program 2
    #create a string variable called x
    x = "number=42"

    #split the string at the '=', resulting in ['number', '42']
    n = x.split('=')[1]

    #turn n from a string into a number, so we could test its value
    n = int(n)

    if 0 < n < 100:
    print "n is between 0 and 100"
    else:
    print "n is not between 0 and 100"
    #-------------------------- End program 2

    And, finally, a program to remove punctuation from a string:

    # ------------------------ Begin program 3
    import string

    #create a sentence with punctuation
    punct = "This. is a, sentence with - punctuation"

    #remove the punctuation; make sure the first argument
    #to maketrans is the same length as the second, which
    #should be all blanks
    punct = punct.translate(string.maketrans('.,-', ' '))

    #read the docs at
    # http://docs.python.org/lib/node109.html
    # for more details
    #------------------------------------End program 3

    Hope this helps; you should be able to put the pieces together to do
    what you want to do. If you can't, feel free to ask more questions.
    Also, just so you know, there is a list at set up
    just to answer questions like these.

    Peace
    Bill Mill
     
    Bill Mill, Jul 13, 2005
    #2
    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