Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]
Groups > comp.lang.python > #42384
| Newsgroups | comp.lang.python |
|---|---|
| Date | 2013-03-31 10:19 -0700 |
| References | <d15c39bc-5d2a-42c9-a76b-23768b61c391@googlegroups.com> <mailman.4018.1364745985.2939.python-list@python.org> |
| Subject | Re: Creating a dictionary from a .txt file |
| From | "C.T." <swilks06@gmail.com> |
| Message-ID | <mailman.4021.1364750406.2939.python-list@python.org> (permalink) |
On Sunday, March 31, 2013 12:06:18 PM UTC-4, Chris Angelico wrote:
> On Mon, Apr 1, 2013 at 2:52 AM, C.T.
>
> > After playing around with the code, I came up with the following code to get everything into a list:
>
> >
>
> > d=[]
>
> > car_file = open('worstcars.txt', 'r')
>
> > for line in car_file:
>
> > d.append(line.strip('\n'))
>
> > print (d)
>
> > car_file.close()
>
> >
>
> > Every line is now an element in list d. The question I have now is how can I make a dictionary out of the list d with the car manufacturer as the key and a tuple containing the year and the model should be the key's value.
>
>
>
> Ah, a nice straight-forward text parsing problem!
>
>
>
> The question is how to recognize the manufacturer. Is it guaranteed to
>
> be the second blank-delimited word, with the year being the first? If
>
> so, you were almost there with .split().
>
>
>
> car_file = open('worstcars.txt', 'r')
>
> # You may want to consider the 'with' statement here - no need to close()
>
> for line in car_file:
>
> temp = line.split(None, 2)
>
> if len(temp)==3:
>
> year, mfg, model = temp
>
> # Now do something with these three values
>
> print("Manufacturer: %s Year: %s Model: %s"%(mfg,year,model))
>
>
>
> That's sorted out the parsing side of things. Do you know how to build
>
> up the dictionary from there?
>
>
>
> What happens if there are multiple entries in the file for the same
>
> manufacturer? Do you need to handle that?
>
>
>
> ChrisA
Thank you, Chris! I could use slicing and indexing to build the dictionary but the problem is with the car manufacturer an the car model. Either or both could be multiple names.
Back to comp.lang.python | Previous | Next — Previous in thread | Next in thread | Find similar | Unroll 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