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


Groups > comp.lang.python > #42378

Re: Creating a dictionary from a .txt file

Path csiph.com!usenet.pasdenom.info!gegeweb.org!de-l.enfer-du-nord.net!feeder1.enfer-du-nord.net!cs.uu.nl!news.stack.nl!newsfeed.xs4all.nl!newsfeed3.news.xs4all.nl!xs4all!post.news.xs4all.nl!not-for-mail
Return-Path <rosuav@gmail.com>
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; 'that?': 0.05; 'element': 0.07; 'subject:file': 0.07; 'parsing': 0.09; 'skip:% 20': 0.09; 'ah,': 0.16; 'from:addr:rosuav': 0.16; 'from:name:chris angelico': 0.16; 'problem!': 0.16; 'subject:.txt': 0.16; 'temp': 0.16; 'there?': 0.16; 'tuple': 0.16; 'wrote:': 0.18; 'year,': 0.18; 'things.': 0.19; 'value.': 0.19; 'code,': 0.22; 'print': 0.22; 'entries': 0.24; 'recognize': 0.24; 'subject: .': 0.24; 'mon,': 0.24; 'question': 0.24; 'second': 0.26; 'values': 0.27; 'header :In-Reply-To:1': 0.27; 'am,': 0.29; 'list:': 0.30; 'statement': 0.30; 'message-id:@mail.gmail.com': 0.30; 'code': 0.31; 'file': 0.32; 'text': 0.33; 'skip:d 20': 0.34; 'subject:from': 0.34; 'received:209.85': 0.35; 'something': 0.35; 'received:google.com': 0.35; 'there': 0.35; 'manufacturer': 0.36; 'should': 0.36; 'so,': 0.37; 'list': 0.37; 'received:209': 0.37; 'being': 0.38; 'skip:o 20': 0.38; 'handle': 0.38; 'to:addr:python-list': 0.38; 'to:addr:python.org': 0.39; 'skip:p 20': 0.39; 'how': 0.40; 'here': 0.66; 'side': 0.67; 'containing': 0.69; 'car': 0.72; 'guaranteed': 0.75; "'with'": 0.84; '2013': 0.98
DKIM-Signature v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20120113; h=mime-version:x-received:in-reply-to:references:date:message-id :subject:from:to:content-type; bh=vyJRjrQO5XVzM0MEJOKXD+7slIOv6cMpGW6IowLbd8g=; b=rn/L1l86Oj853pLPFQlI561XujcOZPoGN8ukW3AeMxZCG010pkzDHBwRpwlW+d3vdp 0Uh6zv3rOjZ5TVtfG5Wcc3uDDDc8EPS1DADF/6ExOZNvP3sw/ZuN9CLP197cS/mSfJaa UZtAdWBSz2UqJhQ4JpRMwqcQPs/J/LJs7iEBW4Q4DAbKcVFbwinl9xqWrKB4Mvimvpfs KX9CV23jHSHaLnCZ/bqNX1pb9Yd18ltZ/GL5Ig4XylkSyJ2qHzF0bbbLW3DEIIPNa6w5 OqVth6VSBhapU+gE17lXsDjwXVIvZJLBuxa6mjuvGfDL0qT7LiFfZgSqjf342cgyWlzg 2HGg==
MIME-Version 1.0
X-Received by 10.52.37.109 with SMTP id x13mr6105680vdj.10.1364745978197; Sun, 31 Mar 2013 09:06:18 -0700 (PDT)
In-Reply-To <d15c39bc-5d2a-42c9-a76b-23768b61c391@googlegroups.com>
References <d15c39bc-5d2a-42c9-a76b-23768b61c391@googlegroups.com>
Date Mon, 1 Apr 2013 03:06:18 +1100
Subject Re: Creating a dictionary from a .txt file
From Chris Angelico <rosuav@gmail.com>
To python-list@python.org
Content-Type text/plain; charset=ISO-8859-1
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.4018.1364745985.2939.python-list@python.org> (permalink)
Lines 34
NNTP-Posting-Host 2001:888:2000:d::a6
X-Trace 1364745985 news.xs4all.nl 6985 [2001:888:2000:d::a6]:45016
X-Complaints-To abuse@xs4all.nl
Xref csiph.com comp.lang.python:42378

Show key headers only | View raw


On Mon, Apr 1, 2013 at 2:52 AM, C.T. <swilks06@gmail.com> wrote:
> 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

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