Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]
Groups > comp.lang.python > #42531
| Path | csiph.com!usenet.pasdenom.info!news.etla.org!news.stack.nl!newsfeed.xs4all.nl!newsfeed3.news.xs4all.nl!xs4all!newsgate.cistron.nl!newsgate.news.xs4all.nl!post.news.xs4all.nl!not-for-mail |
|---|---|
| Return-Path | <davea@davea.name> |
| X-Original-To | python-list@python.org |
| Delivered-To | python-list@mail.python.org |
| X-Spam-Status | OK 0.007 |
| X-Spam-Evidence | '*H*': 0.99; '*S*': 0.00; 'python,': 0.02; 'else:': 0.03; 'elif': 0.05; 'everyone!': 0.07; 'subject:file': 0.07; 'call?': 0.16; 'pythonic': 0.16; 'redundant.': 0.16; 'setdefault': 0.16; 'subject:.txt': 0.16; 'temp': 0.16; 'wrote:': 0.18; 'year,': 0.18; 'all,': 0.19; 'file,': 0.19; 'print': 0.22; 'header:User- Agent:1': 0.23; 'subject: .': 0.24; 'header:In-Reply-To:1': 0.27; 'txt': 0.31; 'another': 0.32; 'there,': 0.34; 'subject:from': 0.34; 'problem': 0.35; 'test': 0.35; 'but': 0.35; 'there': 0.35; 'manufacturer': 0.36; 'doing': 0.36; 'thanks': 0.36; 'thank': 0.38; 'challenging': 0.38; 'handle': 0.38; 'to:addr:python-list': 0.38; 'pm,': 0.38; 'rather': 0.38; 'to:addr:python.org': 0.39; 'manually': 0.60; 'skip:c 50': 0.60; 'most': 0.60; "you're": 0.61; "you've": 0.63; 'received:74.208': 0.68; 'again!': 0.84; 'received:74.208.4.194': 0.84 |
| Date | Mon, 01 Apr 2013 20:12:31 -0400 |
| From | Dave Angel <davea@davea.name> |
| User-Agent | Mozilla/5.0 (X11; Linux x86_64; rv:17.0) Gecko/20130308 Thunderbird/17.0.4 |
| MIME-Version | 1.0 |
| To | python-list@python.org |
| Subject | Re: Creating a dictionary from a .txt file |
| References | <d15c39bc-5d2a-42c9-a76b-23768b61c391@googlegroups.com> <e83bb343-fbed-4d30-ad87-21a99a929f61@googlegroups.com> |
| In-Reply-To | <e83bb343-fbed-4d30-ad87-21a99a929f61@googlegroups.com> |
| Content-Type | text/plain; charset=ISO-8859-1; format=flowed |
| Content-Transfer-Encoding | 7bit |
| X-Provags-ID | V02:K0:W7zgkYUT4F5x9tNHKXZNA9Tyu4BmV63xqODMIwTSz69 du25II63q8Za9F3HpUS3XqK2fA8GY8Xo5s/gOJEHVOXx/wP7I2 RoVSOTbqu51eZk9H5Snp8yCsKLj+BC+B356LLqf33eBS2axPQ7 1QGPfNRe2rPxK1nLGGO7ioIG/90jS5xM9bEp5wDW6n4yoJij50 luKztF5rhNDA6C9897dy+66tsnGtbuLcrhLveTjj8znCR/hgG7 FJ2XIwO7jdRjzldXF9xZo0+HClFtN1T0C4Jsudi/K5fr72Wkb8 XNlpozDkiUWHJo9L6+4QSBcIJPcYGLccYkv9GOSBo7ttEl93Q= = |
| X-BeenThere | python-list@python.org |
| X-Mailman-Version | 2.1.15 |
| Precedence | list |
| List-Id | General discussion list for the Python programming language <python-list.python.org> |
| List-Unsubscribe | <http://mail.python.org/mailman/options/python-list>, <mailto:python-list-request@python.org?subject=unsubscribe> |
| List-Archive | <http://mail.python.org/pipermail/python-list/> |
| List-Post | <mailto:python-list@python.org> |
| List-Help | <mailto:python-list-request@python.org?subject=help> |
| List-Subscribe | <http://mail.python.org/mailman/listinfo/python-list>, <mailto:python-list-request@python.org?subject=subscribe> |
| Newsgroups | comp.lang.python |
| Message-ID | <mailman.26.1364861572.17481.python-list@python.org> (permalink) |
| Lines | 48 |
| NNTP-Posting-Host | 2001:888:2000:d::a6 |
| X-Trace | 1364861572 news.xs4all.nl 6840 [2001:888:2000:d::a6]:53701 |
| X-Complaints-To | abuse@xs4all.nl |
| Xref | csiph.com comp.lang.python:42531 |
Show key headers only | 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 | Next — Previous 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