replacing \n characters in a hash

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

    #1

    replacing \n characters in a hash

    Hello,

    I am currently trying to write some scripts to get information from the
    xmlrpc for redhat network. One of the issues I am having is trying to
    strip off the special characters in the hash that is returned. Here is
    an example of the information returned within the hash :

    ===SNIP===
    {'errata_update _date': '2005-12-06', 'errata_topic': 'Updated
    libc-client packages that fix a buffer overflow issue are
    now\navailable. \n\nThis update has been rated as having moderate
    security impact by the Red\nHat Security Response Team.',
    'errata_type': 'Security Advisory', 'errata_notes': '',
    'errata_synopsi s': 'Moderate: libc-client security update',
    'errata_referen ces': '', 'errata_last_mo dified_date': '2006-01-25
    10:37:24', 'errata_issue_d ate': '2005-12-06', 'errata_descrip tion':
    'C-client is a common API for accessing mailboxes.\n\nA buffer overflow
    flaw was discovered in the way C-client parses user\nsupplied
    mailboxes. If an authenticated user requests a specially
    crafted\nmailbo x name, it may be possible to execute arbitrary code on
    a server that\nuses C-client to access mailboxes. The Common
    Vulnerabilities and Exposures\nproj ect has assigned the name
    CVE-2005-2933 to this issue.\n\nAll users of libc-client should upgrade
    to these updated packages, which\ncontain a backported patch that
    resolves this issue.'}
    ===SNIP===


    What I would like to do is remove the \n characters from
    'errata_topic'. Which is this section of the hash.

    Updated libc-client packages that fix a buffer overflow issue are
    now\navailable. \n\nThis update has been rated as having moderate
    security impact by the Red\nHat Security Response Team.

    What I had attempted to do is use the replace() function but it
    consistantly comes up with the following errors:

    Traceback (most recent call last):
    File "rhn_errata.py" , line 63, in ?
    errata_package = errata_package. strip('\n','')
    AttributeError: 'dict' object has no attribute 'strip'

    where errata_package is JUST the errata_topic hash value.

    Any advice would be great on how to do that.

    Regards,

    Johhny

  • Carsten Haese

    #2
    Re: replacing \n characters in a hash

    On Thu, 2006-01-26 at 09:24, Johhny wrote:[color=blue]
    > Hello,
    >
    > I am currently trying to write some scripts to get information from the
    > xmlrpc for redhat network. One of the issues I am having is trying to
    > strip off the special characters in the hash that is returned. Here is
    > an example of the information returned within the hash :
    >
    > ===SNIP===
    > {'errata_update _date': '2005-12-06', 'errata_topic': 'Updated
    > libc-client packages that fix a buffer overflow issue are
    > now\navailable. \n\nThis update has been rated as having moderate
    > security impact by the Red\nHat Security Response Team.',
    >
    > [...]
    >
    > What I had attempted to do is use the replace() function but it
    > consistantly comes up with the following errors:
    >
    > Traceback (most recent call last):
    > File "rhn_errata.py" , line 63, in ?
    > errata_package = errata_package. strip('\n','')
    > AttributeError: 'dict' object has no attribute 'strip'
    >
    > where errata_package is JUST the errata_topic hash value.[/color]

    errata_package is obviously not just the errata_topic hash value,
    because that would be a string, and python for some reason seems to
    believe that errata_package is a dictionary.

    Also note that once you correct that problem, python will complain that
    strip() doesn't take 2 parameters, since you actually mean replace().

    -Carsten


    Comment

    • Johhny

      #3
      Re: replacing \n characters in a hash

      Hello,

      Thankyou for your response,
      If I check that the errara_package value is with a print I get the
      following.

      ===SNIP===
      Updated libc-client packages that fix a buffer overflow issue are now
      available.

      This update has been rated as having moderate security impact by the
      Red
      Hat Security Response Team.
      ===SNIP===

      Notice that it has formatted the output with the \n's.

      So i dont understand why its reporting as a dictionary rather than just
      the string.

      Comment

      • Carsten Haese

        #4
        Re: replacing \n characters in a hash

        On Thu, 2006-01-26 at 09:49, Johhny wrote:[color=blue]
        > Hello,
        >
        > Thankyou for your response,
        > If I check that the errara_package value is with a print I get the
        > following.
        >
        > ===SNIP===
        > Updated libc-client packages that fix a buffer overflow issue are now
        > available.
        >
        > This update has been rated as having moderate security impact by the
        > Red
        > Hat Security Response Team.
        > ===SNIP===
        >
        > Notice that it has formatted the output with the \n's.
        >
        > So i dont understand why its reporting as a dictionary rather than just
        > the string.[/color]

        Posting relevant bits of your code might help.

        -Carsten


        Comment

        • Johhny

          #5
          Re: replacing \n characters in a hash

          Hello,

          Here is the code (minus my details section).

          server = xmlrpclib.Serve rProxy(url)

          session = server.auth.log in(username,pas sword)

          #functions.

          def getErrata():

          channel_label = 'rhel-i386-as-4'

          errata =
          server.channel. software.list_e rrata(session,c hannel_label,st art_date,end_da te)

          return errata



          def getPackage(advi sory):

          Package = server.errata.g et_details(sess ion,advisory)

          return Package



          errata = getErrata()

          for vals in errata:

          print "%s\t\t%s\t\t%s \t%s\t%s" %
          (vals['errata_advisor y'],vals['errata_issue_d ate'],vals['errata_update_ date'],vals['errata_last_mo dified_date'],vals['errata_type'],)

          errata_info = getPackage(vals['errata_advisor y'],)

          print errata_info['errata_topic']

          errata_package = errata_info['errata_topic']

          print getPackage(vals['errata_advisor y'])

          I have not got any of the section in to replace the \n's as I was
          trying to work out why its not seeing what I thought was a string as a
          dict.

          Comment

          • Larry Bates

            #6
            Re: replacing \n characters in a hash

            Johhny wrote:[color=blue]
            > Hello,
            >
            > I am currently trying to write some scripts to get information from the
            > xmlrpc for redhat network. One of the issues I am having is trying to
            > strip off the special characters in the hash that is returned. Here is
            > an example of the information returned within the hash :
            >
            > ===SNIP===
            > {'errata_update _date': '2005-12-06', 'errata_topic': 'Updated
            > libc-client packages that fix a buffer overflow issue are
            > now\navailable. \n\nThis update has been rated as having moderate
            > security impact by the Red\nHat Security Response Team.',
            > 'errata_type': 'Security Advisory', 'errata_notes': '',
            > 'errata_synopsi s': 'Moderate: libc-client security update',
            > 'errata_referen ces': '', 'errata_last_mo dified_date': '2006-01-25
            > 10:37:24', 'errata_issue_d ate': '2005-12-06', 'errata_descrip tion':
            > 'C-client is a common API for accessing mailboxes.\n\nA buffer overflow
            > flaw was discovered in the way C-client parses user\nsupplied
            > mailboxes. If an authenticated user requests a specially
            > crafted\nmailbo x name, it may be possible to execute arbitrary code on
            > a server that\nuses C-client to access mailboxes. The Common
            > Vulnerabilities and Exposures\nproj ect has assigned the name
            > CVE-2005-2933 to this issue.\n\nAll users of libc-client should upgrade
            > to these updated packages, which\ncontain a backported patch that
            > resolves this issue.'}
            > ===SNIP===
            >
            >
            > What I would like to do is remove the \n characters from
            > 'errata_topic'. Which is this section of the hash.
            >
            > Updated libc-client packages that fix a buffer overflow issue are
            > now\navailable. \n\nThis update has been rated as having moderate
            > security impact by the Red\nHat Security Response Team.
            >
            > What I had attempted to do is use the replace() function but it
            > consistantly comes up with the following errors:
            >
            > Traceback (most recent call last):
            > File "rhn_errata.py" , line 63, in ?
            > errata_package = errata_package. strip('\n','')
            > AttributeError: 'dict' object has no attribute 'strip'
            >
            > where errata_package is JUST the errata_topic hash value.
            >
            > Any advice would be great on how to do that.
            >
            > Regards,
            >
            > Johhny
            >[/color]
            You must use 'errata_topic' as index into your dictionary
            to get the string and then replace the \n chars.

            Example assumes that the dict is pointed to by d):

            errata_topic_te xt=d['errata_topic'].replace('\n',' ')

            Hope this helps.

            Larry Bates

            Comment

            • Fredrik Lundh

              #7
              Re: replacing \n characters in a hash

              Johhny wrote:

              [color=blue]
              > for vals in errata:
              >
              > print "%s\t\t%s\t\t%s \t%s\t%s" %
              >[/color]
              (vals['errata_advisor y'],vals['errata_issue_d ate'],vals['errata_update_ date'],vals['errata_last_mo dified_date'],vals['errata_type'],
              )[color=blue]
              >
              > errata_info = getPackage(vals['errata_advisor y'],)
              >
              > print errata_info['errata_topic']
              >
              > errata_package = errata_info['errata_topic'][/color]

              add

              print type(errata_pac kage), repr(errata_pac kage)

              here, and let us know what it prints.
              [color=blue]
              > print getPackage(vals['errata_advisor y'])[/color]

              </F>



              Comment

              • Steve Holden

                #8
                Re: replacing \n characters in a hash

                Johhny wrote:[color=blue]
                > Hello,
                >
                > Here is the code (minus my details section).
                >
                > server = xmlrpclib.Serve rProxy(url)
                >
                > session = server.auth.log in(username,pas sword)
                >
                > #functions.
                >
                > def getErrata():
                >
                > channel_label = 'rhel-i386-as-4'
                >
                > errata =
                > server.channel. software.list_e rrata(session,c hannel_label,st art_date,end_da te)
                >
                > return errata
                >
                >
                >
                > def getPackage(advi sory):
                >
                > Package = server.errata.g et_details(sess ion,advisory)
                >
                > return Package
                >
                >
                >
                > errata = getErrata()
                >
                > for vals in errata:
                >
                > print "%s\t\t%s\t\t%s \t%s\t%s" %
                > (vals['errata_advisor y'],vals['errata_issue_d ate'],vals['errata_update_ date'],vals['errata_last_mo dified_date'],vals['errata_type'],)
                >
                > errata_info = getPackage(vals['errata_advisor y'],)
                >
                > print errata_info['errata_topic']
                >
                > errata_package = errata_info['errata_topic'][/color]

                errata_package = errata_info['errata_topic'].replace('\n', " ")
                [color=blue]
                >
                > print getPackage(vals['errata_advisor y'])
                >
                > I have not got any of the section in to replace the \n's as I was
                > trying to work out why its not seeing what I thought was a string as a
                > dict.
                >[/color]

                regards
                Steve
                --
                Steve Holden +44 150 684 7255 +1 800 494 3119
                Holden Web LLC www.holdenweb.com
                PyCon TX 2006 www.python.org/pycon/

                Comment

                • Johhny

                  #9
                  Re: replacing \n characters in a hash

                  Hello,

                  In response to that the output is this :

                  <type 'str'> 'Updated libc-client packages that fix a buffer overflow
                  issue are now\navailable. \n\nThis update has been rated as having
                  moderate security impact by the Red\nHat Security Response Team.'

                  Comment

                  • Johhny

                    #10
                    Re: replacing \n characters in a hash

                    Hello All,

                    thanks for your help. I got it working and learnt some more things
                    which is always great. Your assitance has been very useful.

                    Regards,

                    Johhny

                    Comment

                    • Fredrik Lundh

                      #11
                      Re: replacing \n characters in a hash

                      (since this is a newsgroup, can you please quote the message
                      you're replying to).

                      Johhny wrote:
                      [color=blue]
                      > In response to that the output is this :
                      >
                      > <type 'str'> 'Updated libc-client packages that fix a buffer overflow
                      > issue are now\navailable. \n\nThis update has been rated as having
                      > moderate security impact by the Red\nHat Security Response Team.'[/color]

                      <type 'str> means that it *is* a string, after all. looks like you didn't
                      post the code you were using :-(

                      replacing the print statement with

                      errata_package = errata_package. replace("\n", " ")

                      should do what you want.

                      </F>



                      Comment

                      Working...