Python Programming
 
Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
User Name:
Password:
Remember me
Go Back   Dev Shed ForumsProgramming LanguagesPython Programming

Reply
Add This Thread To:
  Del.icio.us   Digg   Google   Spurl   Blink   Furl   Simpy   Y! MyWeb 
Thread Tools Search this Thread Rate Thread Display Modes
 
Unread Dev Shed Forums Sponsor:
Stop making mediocre tutorials.The best tutorials are video! Camtasia Studio makes it easy to create engaging, buzz-building screen videos at any size, in any popular format. Download the free trial!
  #1  
Old May 3rd, 2008, 09:31 PM
maxhit maxhit is offline
Registered User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Apr 2008
Posts: 7 maxhit User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 3 h 2 m 39 sec
Reputation Power: 0
Printing a list desending?

Hello,
I am writing a program somthing like a phonebook and i have a problem.
Im using a list to store the values, its looks somthing like this.
[('641-567-567', 'Bob'), ('656-567-4567', 'joe'), ('567-567-567', 'Billy')]
is it possible to print them going down horizontaly rather than across?
Ex.
('641-567-567', 'Bob')
('656-567-4567', 'joe')
('567-567-567', 'Billy')

When i enter a new number it saves the list in a file, when the program starts it imports the list data.

I tryed a for loop: for entry in phonebook:
print entry
(phonebook being the list)
When i type in new names it works, but the imported ones dont print downwards. Only the new ones.
Thx in advance guys

Reply With Quote
  #2  
Old May 4th, 2008, 03:48 AM
DevCoach DevCoach is offline
Contributing User
Dev Shed Beginner (1000 - 1499 posts)
 
Join Date: Feb 2004
Location: London, England
Posts: 1,174 DevCoach User rank is Captain (20000 - 30000 Reputation Level)DevCoach User rank is Captain (20000 - 30000 Reputation Level)DevCoach User rank is Captain (20000 - 30000 Reputation Level)DevCoach User rank is Captain (20000 - 30000 Reputation Level)DevCoach User rank is Captain (20000 - 30000 Reputation Level)DevCoach User rank is Captain (20000 - 30000 Reputation Level)DevCoach User rank is Captain (20000 - 30000 Reputation Level)DevCoach User rank is Captain (20000 - 30000 Reputation Level)DevCoach User rank is Captain (20000 - 30000 Reputation Level) 
Time spent in forums: 1 Week 5 Days 6 h 3 m 45 sec
Reputation Power: 207
Your code you posted will work regardless of where the list is coming from.

The fact that it does not work means you are doing something else wrong, but without any code to look at I couldn't say what.

Reply With Quote
  #3  
Old May 4th, 2008, 09:25 AM
bvdet bvdet is offline
Registered User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Mar 2008
Posts: 20 bvdet User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 7 h 39 m 4 sec
Reputation Power: 0
I would want my data file to look something like this:
Code:
641-567-5671, Bob
656-567-4567, Joe
567-567-5672, Billy
Read in the data and create a dictionary. The names are the dict keys and the numbers are the values. Iterate on the dictionary for printing.

Python Code:
Original - Python Code
  1. pb_data = open('phone_numbers.txt').read()
  2.  
  3. pb_dict = {}
  4. for item in [s for s in pb_data.split('\n') if s]:
  5.     number, name = item.split(',')
  6.     pb_dict[name.strip()] = number.strip()
  7.  
  8. for name in pb_dict:
  9.     print "%s's phone number is %s" % (name, pb_dict[name])

Reply With Quote
  #4  
Old May 4th, 2008, 01:14 PM
maxhit maxhit is offline
Registered User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Apr 2008
Posts: 7 maxhit User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 3 h 2 m 39 sec
Reputation Power: 0
Quote:
Originally Posted by bvdet
I would want my data file to look something like this:
Code:
641-567-5671, Bob
656-567-4567, Joe
567-567-5672, Billy
Read in the data and create a dictionary. The names are the dict keys and the numbers are the values. Iterate on the dictionary for printing.

Python Code:
Original - Python Code
  1. pb_data = open('phone_numbers.txt').read()
  2.  
  3. pb_dict = {}
  4. for item in [s for s in pb_data.split('\n') if s]:
  5.     number, name = item.split(',')
  6.     pb_dict[name.strip()] = number.strip()
  7.  
  8. for name in pb_dict:
  9.     print "%s's phone number is %s" % (name, pb_dict[name])


Thx alot this helped me alot
But i get this error when running

Traceback (most recent call last):
File "C:\Python25\Compile File\PhoneBook.pyw", line 21, in <module>
number, name = item.split(',')
ValueError: need more than 1 value to unpack

I can post the script if needed(I'd rather not)

Reply With Quote
  #5  
Old May 4th, 2008, 01:34 PM
bvdet bvdet is offline
Registered User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Mar 2008
Posts: 20 bvdet User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 7 h 39 m 4 sec
Reputation Power: 0
item must be a string of the form:
Code:
'567-567-5672, Billy'

Reply With Quote
  #6  
Old May 4th, 2008, 11:43 PM
maxhit maxhit is offline
Registered User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Apr 2008
Posts: 7 maxhit User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 3 h 2 m 39 sec
Reputation Power: 0
Ohh i see
How would i save the file with only that info?
Right now im using pickel to write it.

import pickle
file = open("phone_numbers.dat", "w")
pickle.dump(pb_dict, file)
file.close()

Reply With Quote
  #7  
Old May 5th, 2008, 02:52 AM
SuperOscar SuperOscar is offline
Contributing User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Jul 2007
Location: Tampere, Finland
Posts: 78 SuperOscar User rank is Sergeant (500 - 2000 Reputation Level)SuperOscar User rank is Sergeant (500 - 2000 Reputation Level)SuperOscar User rank is Sergeant (500 - 2000 Reputation Level)SuperOscar User rank is Sergeant (500 - 2000 Reputation Level)SuperOscar User rank is Sergeant (500 - 2000 Reputation Level) 
Time spent in forums: 16 h 50 m 52 sec
Reputation Power: 12
How about plain and simple:

Python Code:
Original - Python Code
  1. fh = open('phone_numbers.dat', 'w')
  2. fh.writelines(['%s\n' % s for s in phonebook])
  3. fh.close()


Edit: Forgot that writelines() doesn’t add newlines, you have to do that yourself.

Last edited by SuperOscar : May 5th, 2008 at 02:55 AM.

Reply With Quote
  #8  
Old May 8th, 2008, 05:38 PM
maxhit maxhit is offline
Registered User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Apr 2008
Posts: 7 maxhit User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 3 h 2 m 39 sec
Reputation Power: 0
Yea! It worked!
Thank you very much SuperOscar and bvdet.

Reply With Quote
Reply

Viewing: Dev Shed ForumsProgramming LanguagesPython Programming > Printing a list desending?


Thread Tools  Search this Thread 
Search this Thread:

Advanced Search
Display Modes  Rate This Thread 
Rate This Thread:


Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
View Your Warnings | New Posts | Latest News | Latest Threads | Shoutbox
Forum Jump

 Free IT White Papers!
 
Accelerating Trading Partner Performance
One in five. That's how many partner transactions have at least one error. That is an amazing statistic, particularly given the extraordinary leaps in innovation across the global supply chain during the past two decades. Download this white paper to learn more.

 
Competing on Analytics
This Tech Analysis is designed to help identify characteristics shared by analytics competitors, and includes information about 32 organizations that have made a commitment to quantitative, fact-based analysis.

 
Cost Effective Scaling with Virtualization and Coyote Point Systems
An overview of the industry trend toward virtualization, how server consolidation has increased the importance of application uptime and the steps being taken to integrate load balancing technology with virtualized servers.

 
Five Checkpoints to Implementing IP Telephony
Implementation planning for IP PBX software and IP telephony has become vital as businesses replace discontinued legacy PBX phone systems. This informative whitepaper outlines five "checkpoints" for any implementation plan that will help make IP communications a successful proposition.

 
Hosted Email Security: Staying Ahead of New Threats
In the last two years, email has become a fierce battleground between the nefarious forces of spam and malware, and the heroes of messaging protection. The spam volumes increased alarmingly every month, bringing clever new forms of phishing and virus propagation attacks.

 

Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
  
 





© 2003-2008 by Developer Shed. All rights reserved. DS Cluster 1 hosted by Hostway