Hexadecimal list conversion

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

    #1

    Hexadecimal list conversion

    Hi All.

    I have a list which is a line from a file:
    ['\x003\x008\x00 1\x004\x007\x00 5\x00.\x005\x00 0\x002\x005\x00 9\x009\x00',
    '\x002\x001\x00 3\x006\x002\x00 2\x00.\x001\x00 7\x004\x002\x00 8\x002\x00']

    This should be in the format:
    ['381475.502599' , '213622.174282']

    I've tried a few options using replace (replacing "\x00" with "") and
    trying to convert from hexademical to decimal.

    But nothing has worked. Can anybody give any tips to help?

    Thanks.

  • Paul Hankin

    #2
    Re: Hexadecimal list conversion

    On Dec 20, 12:30 pm, Neil Webster <nswebs...@gmai l.comwrote:
    Hi All.
    >
    I have a list which is a line from a file:
    ['\x003\x008\x00 1\x004\x007\x00 5\x00.\x005\x00 0\x002\x005\x00 9\x009\x00',
    '\x002\x001\x00 3\x006\x002\x00 2\x00.\x001\x00 7\x004\x002\x00 8\x002\x00']
    >
    This should be in the format:
    ['381475.502599' , '213622.174282']
    >
    I've tried a few options using replace (replacing "\x00" with "") and
    trying to convert from hexademical to decimal.
    >
    But nothing has worked. Can anybody give any tips to help?
    Is your file utf-16 (that would explain why your file has \x00 in
    between every character)? If so, use codecs.open to read it, and you
    won't get the \x00's (you'll get a unicode string).

    Or you can remove them using replace:

    a = a.replace('\x00 ', '')

    HTH
    --
    Paul Hankin

    Comment

    • Andreas Tawn

      #3
      RE: Hexadecimal list conversion

      Hi All.

      I have a list which is a line from a file:
      ['\x003\x008\x00 1\x004\x007\x00 5\x00.\x005\x00 0\x002\x005\x00 9
      \x009\x00',
      '\x002\x001\x00 3\x006\x002\x00 2\x00.\x001\x00 7\x004\x002\x00 8\
      x002\x00']

      This should be in the format:
      ['381475.502599' , '213622.174282']

      I've tried a few options using replace (replacing "\x00" with "") and
      trying to convert from hexademical to decimal.

      But nothing has worked. Can anybody give any tips to help?

      Thanks.
      Somthing like:

      line =
      ['\x003\x008\x00 1\x004\x007\x00 5\x00.\x005\x00 0\x002\x005\x00 9\x009\x00'
      ,
      '\x002\x001\x00 3\x006\x002\x00 2\x00.\x001\x00 7\x004\x002\x00 8\x002\x00']

      result = [''.join(x.split ('\x00')) for x in line]

      Cheers,

      Drea

      Comment

      • Gabriel Genellina

        #4
        Re: Hexadecimal list conversion

        En Thu, 20 Dec 2007 09:30:14 -0300, Neil Webster <nswebster@gmai l.com>
        escribi�:
        I have a list which is a line from a file:
        ['\x003\x008\x00 1\x004\x007\x00 5\x00.\x005\x00 0\x002\x005\x00 9\x009\x00',
        '\x002\x001\x00 3\x006\x002\x00 2\x00.\x001\x00 7\x004\x002\x00 8\x002\x00']
        >
        This should be in the format:
        ['381475.502599' , '213622.174282']
        >
        I've tried a few options using replace (replacing "\x00" with "") and
        trying to convert from hexademical to decimal.
        The replace works:

        pyfor item in L:
        .... print item.replace('\ x00','')
        ....
        381475.502599
        213622.174282

        If you got that from a file, I bet you read it using the wrong encoding.
        Try opening the file using codecs.open("fi lename", "rb",
        encoding="utf-16-be") instead of plain open. When your read it, you'll get
        unicode objects instead of strings, but with the right contents. If you
        wish you can convert to strings using
        line_read.encod e(your_system_e ncoding); if all your data is numeric the
        encoding used is irrelevant and can be omited.

        --
        Gabriel Genellina

        Comment

        • Mark T

          #5
          Re: Hexadecimal list conversion


          "Gabriel Genellina" <gagsl-py2@yahoo.com.a rwrote in message
          news:mailman.26 17.1198155855.1 3605.python-list@python.org ...
          En Thu, 20 Dec 2007 09:30:14 -0300, Neil Webster <nswebster@gmai l.com>
          escribi�:
          >
          >I have a list which is a line from a file:
          >['\x003\x008\x00 1\x004\x007\x00 5\x00.\x005\x00 0\x002\x005\x00 9\x009\x00',
          >'\x002\x001\x0 03\x006\x002\x0 02\x00.\x001\x0 07\x004\x002\x0 08\x002\x00']
          >>
          >This should be in the format:
          >['381475.502599' , '213622.174282']
          >>
          >I've tried a few options using replace (replacing "\x00" with "") and
          >trying to convert from hexademical to decimal.
          >
          The replace works:
          >
          pyfor item in L:
          ... print item.replace('\ x00','')
          ...
          381475.502599
          213622.174282
          >
          If you got that from a file, I bet you read it using the wrong encoding.
          Try opening the file using codecs.open("fi lename", "rb",
          encoding="utf-16-be") instead of plain open. When your read it, you'll get
          unicode objects instead of strings, but with the right contents. If you
          wish you can convert to strings using
          line_read.encod e(your_system_e ncoding); if all your data is numeric the
          encoding used is irrelevant and can be omited.
          >
          --
          Gabriel Genellina
          >
          There is an odd number of bytes in each string. Each begins and ends with
          \x00, so it doesn't look like utf-16-be. But replace works:
          >>L=['\x003\x008\x00 1\x004\x007\x00 5\x00.\x005\x00 0\x002\x005\x00 9\x009\x00','\x 002\x001\x003\x 006\x002\x002\x 00.\x001\x007\x 004\x002\x008\x 002\x00']
          >>[s.replace('\x00 ','') for s in L]
          ['381475.502599' , '213622.174282']

          -Mark Tolonen

          Comment

          • Peter Otten

            #6
            Re: Hexadecimal list conversion

            Mark T wrote:
            "Gabriel Genellina" <gagsl-py2@yahoo.com.a rwrote in message
            >If you got that from a file, I bet you read it using the wrong
            >encoding. Try opening the file using codecs.open("fi lename", "rb",
            >encoding="ut f-16-be") instead of plain open.
            There is an odd number of bytes in each string. Each begins and ends
            with \x00, so it doesn't look like utf-16-be.
            I think Gabriel is right. The OP probably butchered the original structure
            with

            open(filename). read().split("\ n")

            Peter

            Comment

            • John Machin

              #7
              Re: Hexadecimal list conversion

              On Dec 21, 2:51 am, Peter Otten <__pete...@web. dewrote:
              Mark T wrote:
              "Gabriel Genellina" <gagsl-...@yahoo.com.a rwrote in message
              If you got that from a file, I bet you read it using the wrong
              encoding. Try opening the file using codecs.open("fi lename", "rb",
              encoding="utf-16-be") instead of plain open.
              There is an odd number of bytes in each string. Each begins and ends
              with \x00, so it doesn't look like utf-16-be.
              >
              I think Gabriel is right. The OP probably butchered the original structure
              with
              >
              open(filename). read().split("\ n")
              Or he's read the file "normally" and then done
              line = lineZAP
              where ZAP is one of [:-1], .rstrip(), .rstrip("\n"), etc

              However that accounts only for the rightmost trailing \x00. Looks like
              each line has been chainsawed with .split(",") or whatever the
              original field separator was.

              If Gabriel's instructions don't "work" for the OP, the OP should show
              us an unambiguous representation of the first few bytes of the
              original file, instead of leaving it to guesswork:

              print repr(open("the_ file", "rb").read( )[:200])

              Comment

              • Gabriel Genellina

                #8
                Re: Hexadecimal list conversion

                En Thu, 20 Dec 2007 12:51:33 -0300, Peter Otten <__peter__@web. de>
                escribió:
                Mark T wrote:
                >
                >"Gabriel Genellina" <gagsl-py2@yahoo.com.a rwrote in message
                >
                >>If you got that from a file, I bet you read it using the wrong
                >>encoding. Try opening the file using codecs.open("fi lename", "rb",
                >>encoding="u tf-16-be") instead of plain open.
                >
                >There is an odd number of bytes in each string. Each begins and ends
                >with \x00, so it doesn't look like utf-16-be.
                >
                I think Gabriel is right. The OP probably butchered the original
                structure
                with
                >
                open(filename). read().split("\ n")
                Sure! I take bets on this too.

                --
                Gabriel Genellina

                Comment

                Working...