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


Groups > comp.lang.python > #42380

Re: Creating a dictionary from a .txt file

From Roy Smith <roy@panix.com>
Newsgroups comp.lang.python
Subject Re: Creating a dictionary from a .txt file
Date 2013-03-31 12:38 -0400
Organization PANIX Public Access Internet and UNIX, NYC
Message-ID <roy-7B35BF.12385631032013@news.panix.com> (permalink)
References <d15c39bc-5d2a-42c9-a76b-23768b61c391@googlegroups.com>

Show all headers | View raw


In article <d15c39bc-5d2a-42c9-a76b-23768b61c391@googlegroups.com>,
 "C.T." <swilks06@gmail.com> wrote:

> Hello,
> 
> I'm currently working on a homework problem that requires me to create a 
> dictionary from a .txt file that contains some of the worst cars ever made. 
> The file looks something like this:
> 
> 1958 MGA Twin Cam
> 1958 Zunndapp Janus
> 1961 Amphicar
> 1961 Corvair
> 1966 Peel Trident
> 1970 AMC Gremlin
> 1970 Triumph Stag
> 1971 Chrysler Imperial LeBaron Two-Door Hardtop
> 
> The car manufacturer should be the key and a tuple containing the year and 
> the model should be the key's value. I tried the following to just get the 
> contents of the file into a list, but only the very last line in the txt file 
> is shown as a list with three elements (ie, ['2004', 'Chevy', 'SSR']) when I 
> print temp.
> 
> d={}
> car_file = open('worstcars.txt', 'r')
> for line in car_file:
>     temp = line.split()
> print (temp)
> car_file.close()

Yup.  Because you run through the whole file, putting each line into 
temp, overwriting the previous temp value.

> d=[]
> car_file = open('worstcars.txt', 'r')
> for line in car_file:
>     d.append(line.strip('\n'))
> print (d)
> car_file.close()

You could do most of that with just:

car_file = open('worstcars.txt', 'r')
d = car_file.readlines()

but there's no real reason to read the whole file into a list.  What you 
probably want to do is something like:

d = {}
car_file = open('worstcars.txt', 'r')
for line in car_file:
   year, manufacturer, model = parse_line(line)
   d[manufacturer] = (year, model)

One comment about the above; it assumes that there's only a single entry 
for a given manufacturer in the file.  If that's not true, the above 
code will only keep the last one.  But let's assume it's true for the 
moment.

Now, we're just down to writing parse_line().  This takes a string and 
breaks it up into 3 strings.  I'm going to leave this as an exercise for 
you to work out.  The complicated part is going to be figuring out some 
logic to deal with anything from multi-word model names ("Imperial 
LeBaron Two-Door Hardtop"), to lines like the Corvair where there is no 
manufacturer (or maybe there's no model?).

Back to comp.lang.python | Previous | NextPrevious in thread | Next 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