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: 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 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: In-Reply-To: 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 List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Newsgroups: comp.lang.python Message-ID: 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 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