Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.python > #42531

Re: Creating a dictionary from a .txt file

Date 2013-04-01 20:12 -0400
From Dave Angel <davea@davea.name>
Subject Re: Creating a dictionary from a .txt file
References <d15c39bc-5d2a-42c9-a76b-23768b61c391@googlegroups.com> <e83bb343-fbed-4d30-ad87-21a99a929f61@googlegroups.com>
Newsgroups comp.lang.python
Message-ID <mailman.26.1364861572.17481.python-list@python.org> (permalink)

Show all headers | View raw


On 04/01/2013 07:53 PM, C.T. wrote:
> Thanks for all the help everyone! After I manually edited the txt file, this is what I came up with:
>
> car_dict = {}
> car_file = open('cars.txt', 'r')
>
>
>
> for line in car_file:
>      temp = line.strip().split(None, 2)
>      temp2 = line.strip().split('\t')
>
>
>      if len(temp)==3:
>          year, manufacturer, model = temp[0] ,temp2[0][5:], temp2[1]
>          value = (year, model)
>          if manufacturer in car_dict:
>              car_dict.setdefault(manufacturer,[]).append(value)

That's rather redundant.  Once you've determined that the particular key 
is already there, why bother with the setdefault() call?  Or to put it 
another way, why bother to test if it's there when you're going to use 
setdefault to handle the case where it's not?


>          else:
>              car_dict[manufacturer] = [value]
>
>
>      elif len(temp)==2:
>          year, manufacturer, model = temp[0], 'Unknown' , temp2[1]
>          value = (year, model)
>          if manufacturer in car_dict:
>              car_dict.setdefault(manufacturer,[]).append(value)
>          else:
>              car_dict[manufacturer] = [value]
>
>
> car_file.close()
>
> print (car_dict)
>
> It may not be the most pythonic way of doing this, but it works for me. I am learning python, and this problem was problem the most challenging so far. Thank you all, again!
>


-- 
DaveA

Back to comp.lang.python | Previous | NextPrevious in thread | Find similar | Unroll thread


Thread

Creating a dictionary from a .txt file "C.T." <swilks06@gmail.com> - 2013-03-31 08:52 -0700
  Re: Creating a dictionary from a .txt file Chris Angelico <rosuav@gmail.com> - 2013-04-01 03:06 +1100
    Re: Creating a dictionary from a .txt file "C.T." <swilks06@gmail.com> - 2013-03-31 10:19 -0700
      Re: Creating a dictionary from a .txt file Chris Angelico <rosuav@gmail.com> - 2013-04-01 04:22 +1100
    Re: Creating a dictionary from a .txt file "C.T." <swilks06@gmail.com> - 2013-03-31 10:19 -0700
  Re: Creating a dictionary from a .txt file Mark Janssen <dreamingforward@gmail.com> - 2013-03-31 09:20 -0700
    Re: Creating a dictionary from a .txt file "C.T." <swilks06@gmail.com> - 2013-03-31 09:52 -0700
      Re: Creating a dictionary from a .txt file Dave Angel <davea@davea.name> - 2013-03-31 13:31 -0400
        Re: Creating a dictionary from a .txt file Roy Smith <roy@panix.com> - 2013-03-31 14:41 -0400
          Re: Creating a dictionary from a .txt file Dave Angel <davea@davea.name> - 2013-03-31 17:37 -0400
          Re: Creating a dictionary from a .txt file Neil Cerutti <neilc@norwich.edu> - 2013-04-01 11:41 +0000
            Re: Creating a dictionary from a .txt file Steven D'Aprano <steve+comp.lang.python@pearwood.info> - 2013-04-01 23:53 +0000
              Re: Creating a dictionary from a .txt file Walter Hurry <walterhurry@lavabit.com> - 2013-04-02 00:28 +0000
              Re: Creating a dictionary from a .txt file Neil Cerutti <neilc@norwich.edu> - 2013-04-03 12:55 +0000
    Re: Creating a dictionary from a .txt file "C.T." <swilks06@gmail.com> - 2013-03-31 09:52 -0700
  Re: Creating a dictionary from a .txt file Roy Smith <roy@panix.com> - 2013-03-31 12:38 -0400
    Re: Creating a dictionary from a .txt file "C.T." <swilks06@gmail.com> - 2013-03-31 10:28 -0700
  Re: Creating a dictionary from a .txt file Terry Jan Reedy <tjreedy@udel.edu> - 2013-03-31 15:04 -0400
  Re: Creating a dictionary from a .txt file "C.T." <swilks06@gmail.com> - 2013-04-01 16:53 -0700
    Re: Creating a dictionary from a .txt file Dave Angel <davea@davea.name> - 2013-04-01 20:12 -0400

csiph-web