text manipulation

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Johhny

    #1

    text manipulation

    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_v ariable = no
    #

    I would like to make that

    #comment here
    user_defined_va riable = yes
    #

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

    Regards,

    Johhny.

  • Isaac T Alston

    #2
    Re: text manipulation

    Johhny wrote:
    [color=blue]
    > Any advice would be great.[/color]


    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

    Comment

    • Martin Franklin

      #3
      Re: text manipulation

      Johhny wrote:[color=blue]
      > 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_v ariable = no
      > #
      >
      > I would like to make that
      >
      > #comment here
      > user_defined_va riable = yes
      > #
      >
      > With perl/sed Its very easy, However Im having issues to do it in
      > python. Any advice would be great.
      >
      > Regards,
      >
      > Johhny.
      >[/color]


      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_va riable = no",
      "user_defined_v ariable = yes")

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


      Cheers
      Martin

      Comment

      • Juho Schultz

        #4
        Re: text manipulation

        Johhny wrote:[color=blue]
        > 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_v ariable = no
        > #
        >
        > I would like to make that
        >
        > #comment here
        > user_defined_va riable = yes
        > #
        >
        > With perl/sed Its very easy, However Im having issues to do it in
        > python. Any advice would be great.
        >
        > Regards,
        >
        > Johhny.
        >[/color]

        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 = {'userdefinedva r1':'yes',
        'userdefinedvar 2':'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_k ey(name):
        # replace line
        output.append(" %s = %s" % (name, newvalue[name]))
        else:
        # or just uncomment
        output.append(" %s = %s" % (name, oldvalue))
        else:
        output.append(l ine)

        # write output to file

        Comment

        • Martin Franklin

          #5
          Re: text manipulation

          Martin Franklin wrote:[color=blue]
          > Johhny wrote:
          >[color=green]
          >>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.
          >>[/color]
          >
          >
          >
          > 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_va riable = no",
          > "user_defined_v ariable = yes")
          >
          > ... continue processing file / writing out stuff as you go
          >
          >
          > Cheers
          > Martin
          >[/color]




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

          for line in file:
          if line.startswith ("#user_defined _variable = no"):
          line = line.replace("# user_defined_va riable = no",
          "user_defined_v ariable = yes")

          Comment

          • BartlebyScrivener

            #6
            Re: text manipulation

            John,

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



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

            rpd

            Comment

            Working...