text manipulation

Discussion in 'Python' started by Johhny, Dec 16, 2005.

  1. Johhny

    Johhny Guest

    Hello,

    I am trying to write a script in python (to replace a perl script with
    limited functionality). Now I have some issues. Currently I am using
    the perl to load the file then regex parse certain lines to remove
    characters (uncomment lines and change variables). I would like to take
    that into the python script. I have had a look at the module "string"
    and I dont think its what Im looking for.

    Here is an example of some text I would like to manipulate

    #comment here
    #user_defined_variable = no
    #

    I would like to make that

    #comment here
    user_defined_variable = yes
    #

    With perl/sed Its very easy, However Im having issues to do it in
    python. Any advice would be great.

    Regards,

    Johhny.
     
    Johhny, Dec 16, 2005
    #1
    1. Advertising

  2. Johhny wrote:

    > Any advice would be great.



    Have you had a look at Python's re module? If you've not, I suggest you do
    so - it contains all of Python's regex tools which you'll need.

    Regards,

    --
    Isaac
     
    Isaac T Alston, Dec 16, 2005
    #2
    1. Advertising

  3. Johhny wrote:
    > Hello,
    >
    > I am trying to write a script in python (to replace a perl script with
    > limited functionality). Now I have some issues. Currently I am using
    > the perl to load the file then regex parse certain lines to remove
    > characters (uncomment lines and change variables). I would like to take
    > that into the python script. I have had a look at the module "string"
    > and I dont think its what Im looking for.
    >
    > Here is an example of some text I would like to manipulate
    >
    > #comment here
    > #user_defined_variable = no
    > #
    >
    > I would like to make that
    >
    > #comment here
    > user_defined_variable = yes
    > #
    >
    > With perl/sed Its very easy, However Im having issues to do it in
    > python. Any advice would be great.
    >
    > Regards,
    >
    > Johhny.
    >



    forget regular expressions for this job... strings have methods in
    python so for example:


    for line in file:
    if line.startswith("#user_defined_variable = no"):
    line.replace("#user_defined_variable = no",
    "user_defined_variable = yes")

    .... continue processing file / writing out stuff as you go


    Cheers
    Martin
     
    Martin Franklin, Dec 16, 2005
    #3
  4. Johhny

    Juho Schultz Guest

    Johhny wrote:
    > Hello,
    >
    > I am trying to write a script in python (to replace a perl script with
    > limited functionality). Now I have some issues. Currently I am using
    > the perl to load the file then regex parse certain lines to remove
    > characters (uncomment lines and change variables). I would like to take
    > that into the python script. I have had a look at the module "string"
    > and I dont think its what Im looking for.
    >
    > Here is an example of some text I would like to manipulate
    >
    > #comment here
    > #user_defined_variable = no
    > #
    >
    > I would like to make that
    >
    > #comment here
    > user_defined_variable = yes
    > #
    >
    > With perl/sed Its very easy, However Im having issues to do it in
    > python. Any advice would be great.
    >
    > Regards,
    >
    > Johhny.
    >


    Have you also looked at the built-in string methods?


    The following script is probably something you
    could start with - it just uncomments lines
    and replaces values of some variables.

    # dictionary of the variable names
    # contains the replacement values
    newvalues = {'userdefinedvar1':'yes',
    'userdefinedvar2':'123.654.345.234'}

    # read file to input
    output = []
    for line in input:
    # check if it is a comment with "="
    if line.startswith('#') and ('=' in line):
    name, oldvalue = (line.lstrip('#')).split('=')
    # line.lstrip removes leading comments,
    # split('=') finds the assignment
    if newvalues.has_key(name):
    # replace line
    output.append("%s = %s" % (name, newvalue[name]))
    else:
    # or just uncomment
    output.append("%s = %s" % (name, oldvalue))
    else:
    output.append(line)

    # write output to file
     
    Juho Schultz, Dec 16, 2005
    #4
  5. Martin Franklin wrote:
    > Johhny wrote:
    >
    >>Hello,
    >>
    >>I am trying to write a script in python (to replace a perl script with
    >>limited functionality). Now I have some issues. Currently I am using
    >>the perl to load the file then regex parse certain lines to remove
    >>characters (uncomment lines and change variables). I would like to take
    >>that into the python script. I have had a look at the module "string"
    >>and I dont think its what Im looking for.
    >>
    >>Here is an example of some text I would like to manipulate
    >>
    >>#comment here
    >>#user_defined_variable = no
    >>#
    >>
    >>I would like to make that
    >>
    >>#comment here
    >>user_defined_variable = yes
    >>#
    >>
    >>With perl/sed Its very easy, However Im having issues to do it in
    >>python. Any advice would be great.
    >>
    >>Regards,
    >>
    >>Johhny.
    >>

    >
    >
    >
    > forget regular expressions for this job... strings have methods in
    > python so for example:
    >
    >
    > for line in file:
    > if line.startswith("#user_defined_variable = no"):
    > line.replace("#user_defined_variable = no",
    > "user_defined_variable = yes")
    >
    > ... continue processing file / writing out stuff as you go
    >
    >
    > Cheers
    > Martin
    >





    whoops forgot string methods return the new string.....

    for line in file:
    if line.startswith("#user_defined_variable = no"):
    line = line.replace("#user_defined_variable = no",
    "user_defined_variable = yes")
     
    Martin Franklin, Dec 16, 2005
    #5
  6. John,

    Martin is right. Always try to solve without regex first. However, for
    those situations where you definitely need regex, use this tutorial:

    http://www.amk.ca/python/howto/regex/

    It's well-written, succinct, and Python-specific.

    rpd
     
    BartlebyScrivener, Dec 16, 2005
    #6
    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. Mohamed Ahmed Meligy

    Text Manipulation

    Mohamed Ahmed Meligy, Jun 17, 2004, in forum: ASP .Net
    Replies:
    1
    Views:
    371
    Hans Kesting
    Jun 17, 2004
  2. jack-e

    text manipulation

    jack-e, Aug 9, 2005, in forum: ASP .Net
    Replies:
    3
    Views:
    359
    Oliver Wong
    Aug 9, 2005
  3. Tim Peters
    Replies:
    0
    Views:
    528
    Tim Peters
    Jan 24, 2004
  4. ProvoWallis

    NewB question on text manipulation

    ProvoWallis, May 3, 2006, in forum: Python
    Replies:
    4
    Views:
    350
    ProvoWallis
    May 3, 2006
  5. Rex
    Replies:
    2
    Views:
    304
Loading...

Share This Page