Path: csiph.com!usenet.pasdenom.info!aioe.org!news.stack.nl!newsfeed.xs4all.nl!newsfeed1.news.xs4all.nl!xs4all!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.002 X-Spam-Evidence: '*H*': 1.00; '*S*': 0.00; 'made.': 0.07; 'subject:file': 0.07; 'maker': 0.09; 'parsing': 0.09; 'received:80.91': 0.09; 'received:80.91.229': 0.09; 'received:gmane.org': 0.09; 'received:list': 0.09; '.txt': 0.16; 'line.split()': 0.16; 'received:80.91.229.3': 0.16; 'received:plane.gmane.org': 0.16; 'subject:.txt': 0.16; 'temp': 0.16; 'tuple': 0.16; 'twin': 0.16; 'worst': 0.16; 'elements': 0.16; 'wrote:': 0.18; 'split': 0.19; 'value.': 0.19; 'print': 0.22; 'header:User-Agent:1': 0.23; 'subject: .': 0.24; 'looks': 0.24; 'shown': 0.26; 'this:': 0.26; 'header:X-Complaints-To:1': 0.27; 'header:In-Reply-To:1': 0.27; 'tried': 0.27; 'am,': 0.29; "i'm": 0.30; 'txt': 0.31; 'file': 0.32; 'subject:from': 0.34; 'problem': 0.35; 'something': 0.35; 'but': 0.35; 'manufacturer': 0.36; 'words,': 0.36; 'should': 0.36; 'list': 0.37; 'skip:o 20': 0.38; 'to:addr:python-list': 0.38; 'list,': 0.38; 'to:addr:python.org': 0.39; 'received:org': 0.40; 'received:173': 0.61; 'containing': 0.69; 'car': 0.72; 'cars': 0.84; 'homework': 0.84; 'received:fios.verizon.net': 0.84; '1970': 0.91 X-Injected-Via-Gmane: http://gmane.org/ To: python-list@python.org From: Terry Jan Reedy Subject: Re: Creating a dictionary from a .txt file Date: Sun, 31 Mar 2013 15:04:54 -0400 References: Mime-Version: 1.0 Content-Type: text/plain; charset=ISO-8859-1; format=flowed Content-Transfer-Encoding: 7bit X-Gmane-NNTP-Posting-Host: pool-173-75-251-66.phlapa.fios.verizon.net User-Agent: Mozilla/5.0 (Windows NT 6.1; WOW64; rv:17.0) Gecko/20130307 Thunderbird/17.0.4 In-Reply-To: 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: 27 NNTP-Posting-Host: 2001:888:2000:d::a6 X-Trace: 1364756707 news.xs4all.nl 6881 [2001:888:2000:d::a6]:56297 X-Complaints-To: abuse@xs4all.nl Xref: csiph.com comp.lang.python:42392 On 3/31/2013 11:52 AM, C.T. 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() If all makers are one word (Austen-Martin would be ok, and if the file is otherwise consistently year maker model words, then adding 'maxsplit=3' to the split call would be all the parsing you need.