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


Groups > comp.lang.python > #42392

Re: Creating a dictionary from a .txt file

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 <python-python-list@m.gmane.org>
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 <tjreedy@udel.edu>
Subject Re: Creating a dictionary from a .txt file
Date Sun, 31 Mar 2013 15:04:54 -0400
References <d15c39bc-5d2a-42c9-a76b-23768b61c391@googlegroups.com>
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 <d15c39bc-5d2a-42c9-a76b-23768b61c391@googlegroups.com>
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.4024.1364756707.2939.python-list@python.org> (permalink)
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

Show key headers only | View raw


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.


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