csv dictreader

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

    #1

    csv dictreader

    I am trying to use the dictionary reader to import the data from a csv
    file and create a dictnary from it but just can't seem to figure it
    out.

    Here is my code:
    >>>import csv
    >>>reader = csv.DictReader( open('table.csv '))
    >>>for row in reader:
    >>>print row
    my csv files looks like this:

    Bytecode,Elemen t
    \x00\x00,0000
    \x01\x00,0001
    .....
    \x09\x00,0009

    My output shows:
    {'Bytecode': '\\x00\\x00', 'Element': '0000'}
    {'Bytecode': '\\x01\\x00', 'Element': '0001'}
    ....
    {'Bytecode': '\\x09\\x00', 'Element': '0009'}

    1. how can I get access to this directory
    2. why does the values come with two backslashs infront of the "x"
  • Mike Driscoll

    #2
    Re: csv dictreader

    On Mar 19, 1:06 pm, brnstrmrs <brnstr...@gmai l.comwrote:
    I am trying to use the dictionary reader to import the data from a csv
    file and create a dictnary from it but just can't seem to figure it
    out.
    >
    Here is my code:
    >
    >>import csv
    >>reader = csv.DictReader( open('table.csv '))
    >>for row in reader:
    >>print row
    >
    my csv files looks like this:
    >
    Bytecode,Elemen t
    \x00\x00,0000
    \x01\x00,0001
    ....
    \x09\x00,0009
    >
    My output shows:
    {'Bytecode': '\\x00\\x00', 'Element': '0000'}
    {'Bytecode': '\\x01\\x00', 'Element': '0001'}
    ...
    {'Bytecode': '\\x09\\x00', 'Element': '0009'}
    >
    1. how can I get access to this directory
    What do you mean? You can get each element in the dict by doing this:

    for row in reader:
    print row['Bytecode']
    print row['Element']

    2. why does the values come with two backslashs infront of the "x"
    The double back slash is for escaping purposes, I think. If you print
    this: '\\x09\\x00'
    you'll get this: \x09\x00

    Mike

    Comment

    • brnstrmrs

      #3
      Re: csv dictreader

      On Mar 19, 2:32 pm, Mike Driscoll <kyoso...@gmail .comwrote:
      On Mar 19, 1:06 pm, brnstrmrs <brnstr...@gmai l.comwrote:
      >
      >
      >
      I am trying to use the dictionary reader to import the data from a csv
      file and create a dictnary from it but just can't seem to figure it
      out.
      >
      Here is my code:
      >
      >>>import csv
      >>>reader = csv.DictReader( open('table.csv '))
      >>>for row in reader:
      >>>print row
      >
      my csv files looks like this:
      >
      Bytecode,Elemen t
      \x00\x00,0000
      \x01\x00,0001
      ....
      \x09\x00,0009
      >
      My output shows:
      {'Bytecode': '\\x00\\x00', 'Element': '0000'}
      {'Bytecode': '\\x01\\x00', 'Element': '0001'}
      ...
      {'Bytecode': '\\x09\\x00', 'Element': '0009'}
      >
      1. how can I get access to this directory
      >
      What do you mean? You can get each element in the dict by doing this:
      >
      for row in reader:
      print row['Bytecode']
      print row['Element']
      >
      2. why does the values come with two backslashs infront of the "x"
      >
      The double back slash is for escaping purposes, I think. If you print
      this: '\\x09\\x00'
      you'll get this: \x09\x00
      >
      Mike
      Thanks. It works for the Bytecode but when I do print row['Element']
      I get a error message print row['Element'] KeyError: 'Element'

      Comment

      • Mike Driscoll

        #4
        Re: csv dictreader

        On Mar 19, 1:55 pm, brnstrmrs <brnstr...@gmai l.comwrote:
        On Mar 19, 2:32 pm, Mike Driscoll <kyoso...@gmail .comwrote:
        >
        >
        >
        On Mar 19, 1:06 pm, brnstrmrs <brnstr...@gmai l.comwrote:
        >
        I am trying to use the dictionary reader to import the data from a csv
        file and create a dictnary from it but just can't seem to figure it
        out.
        >
        Here is my code:
        >
        >>import csv
        >>reader = csv.DictReader( open('table.csv '))
        >>for row in reader:
        >>print row
        >
        my csv files looks like this:
        >
        Bytecode,Elemen t
        \x00\x00,0000
        \x01\x00,0001
        ....
        \x09\x00,0009
        >
        My output shows:
        {'Bytecode': '\\x00\\x00', 'Element': '0000'}
        {'Bytecode': '\\x01\\x00', 'Element': '0001'}
        ...
        {'Bytecode': '\\x09\\x00', 'Element': '0009'}
        >
        1. how can I get access to this directory
        >
        What do you mean? You can get each element in the dict by doing this:
        >
        for row in reader:
        print row['Bytecode']
        print row['Element']
        >
        2. why does the values come with two backslashs infront of the "x"
        >
        The double back slash is for escaping purposes, I think. If you print
        this: '\\x09\\x00'
        you'll get this: \x09\x00
        >
        Mike
        >
        Thanks. It works for the Bytecode but when I do print row['Element']
        I get a error message print row['Element'] KeyError: 'Element'
        That's weird. Since I only had your output, I did the following:

        <code>

        reader = [ {'Bytecode': '\\x00\\x00', 'Element': '0000'},{'Bytec ode':
        '\\x01\\x00', 'Element': '0001'} ]

        for x in reader:
        print x['Element']
        print x['Bytecode']


        0000
        \x00\x00
        0001
        \x01\x00

        </code>

        I must be missing something...

        Mike

        Comment

        • Dravidan

          #5
          Re: csv dictreader

          It looks like the backslash is messing the whole thing up so I
          reposted to try to find out how to get over the backslash hump.


          On Mar 20, 2:08 am, Dennis Lee Bieber <wlfr...@ix.net com.comwrote:
          On Wed, 19 Mar 2008 11:06:40 -0700 (PDT), brnstrmrs
          <brnstr...@gmai l.comdeclaimed the following in comp.lang.pytho n:
          >
          my csv files looks like this:
          >
          Bytecode,Elemen t
          \x00\x00,0000
          \x01\x00,0001
          ....
          \x09\x00,0009
          >
          I sure hope your data is more complex than that... otherwise it's a
          waste of space...
          >
          >>for i in range(10):
          ... print i, repr(struct.pac k("h", i))
          ...
          0 '\x00\x00'
          1 '\x01\x00'
          2 '\x02\x00'
          3 '\x03\x00'
          4 '\x04\x00'
          5 '\x05\x00'
          6 '\x06\x00'
          7 '\x07\x00'
          8 '\x08\x00'
          9 '\t\x00'
          >
          And, \t is the same value \x09 (a tab character)
          --
          Wulfraed Dennis Lee Bieber KD6MOG
          wlfr...@ix.netc om.com wulfr...@bestia ria.com

          (Bestiaria Support Staff: web-a...@bestiaria. com)
          HTTP://www.bestiaria.com/

          Comment

          Working...