Win a copy of Terraform in Action this week in the Cloud forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Tim Cooke
  • Campbell Ritchie
  • Paul Clapham
  • Ron McLeod
  • Liutauras Vilda
Sheriffs:
  • Jeanne Boyarsky
  • Rob Spoor
  • Bear Bibeault
Saloon Keepers:
  • Jesse Silverman
  • Tim Moores
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
Bartenders:
  • Piet Souris
  • Al Hobbs
  • salvin francis

Reading from files and parsing data efficiently

 
village idiot
Posts: 1208
1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I'm back! Okay, I've got this text file with a list of data for 85 restaurants. The data is organized for each in this order: name, rating, price range, cuisine type. I am supposed to write a function that reads this file and returns a tuple of three dictionaries. I used file.readlines() to read the file in, then I stripped out all of the \n characters. This left me with list of data separated by an empty string. I created the first dictionary by iterating through the indexes of the file, creating keys and values, but the other two dictionaries involved keys that had lists for values, and I just got stuck, so I populated them manually. I'm sure I'm supposed to create the dictionaries by reading the file, but I haven't been able to come up with code that works. What can I try to build these dictionaries by reading the file? I need a little nudge in the right direction.

Eureka!



This populates my first list. These nested loops require inside-out thinking, and it takes me a while to sort hem out. (That and I used Python Visualizer to step through the code. Nice little tool!)
 
Carol Murphy
village idiot
Posts: 1208
1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
See above!
 
You showed up just in time for the waffles! And this tiny ad:
Building a Better World in your Backyard by Paul Wheaton and Shawn Klassen-Koop
https://coderanch.com/wiki/718759/books/Building-World-Backyard-Paul-Wheaton
reply
    Bookmark Topic Watch Topic
  • New Topic