Python Programming
 
Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
User Name:
Password:
Remember me

The Shed is going Social! Join us on FaceBook and Twitter and chime in on the conversation.

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:
  #1  
Old February 24th, 2004, 06:47 AM
flixxer flixxer is offline
Contributing User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Feb 2004
Posts: 44 flixxer User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: < 1 sec
Reputation Power: 10
Accessing text file

If i have a text file with these value:

Code:
## -- Text file --##

nickname:realname
something:anythin

##--- Text file --##


how can I display the value of eg. nickname based on user input(eg: if the user type in nickname the the screen will display the real name)

Reply With Quote
  #2  
Old February 24th, 2004, 07:51 AM
sfb sfb is offline
Contributing User
Dev Shed Novice (500 - 999 posts)
 
Join Date: Nov 2003
Posts: 623 sfb User rank is Sergeant Major (2000 - 5000 Reputation Level)sfb User rank is Sergeant Major (2000 - 5000 Reputation Level)sfb User rank is Sergeant Major (2000 - 5000 Reputation Level)sfb User rank is Sergeant Major (2000 - 5000 Reputation Level)sfb User rank is Sergeant Major (2000 - 5000 Reputation Level)sfb User rank is Sergeant Major (2000 - 5000 Reputation Level) 
Time spent in forums: 3 Days 5 h 53 m 21 sec
Reputation Power: 33
Read the lines of the file, and store them somewhere. Then just lookup in the store when someone types something.
If you only want one-way lookups then a Dictionary is good to use.

Code:
pairs = {}

for line in file('pairs.txt'):
    try:
        x, y = line.split(':')
        pairs[x] = y
    except: 
        #Ignore empty lines, etc.
        pass

try:
    val = raw_input('Type something: ')
    print val, "links to", pairs[val]
except KeyError:
    print val, "not found in pairs list"

Reply With Quote
  #3  
Old February 24th, 2004, 09:54 AM
Grim Archon's Avatar
Grim Archon Grim Archon is offline
Mini me.
Dev Shed Novice (500 - 999 posts)
 
Join Date: Nov 2003
Location: Cambridge, UK
Posts: 783 Grim Archon User rank is Corporal (100 - 500 Reputation Level)Grim Archon User rank is Corporal (100 - 500 Reputation Level)Grim Archon User rank is Corporal (100 - 500 Reputation Level)Grim Archon User rank is Corporal (100 - 500 Reputation Level)  Folding Points: 1488 Folding Title: Novice Folder
Time spent in forums: 3 Days 2 h 15 m 57 sec
Reputation Power: 12
Send a message via MSN to Grim Archon
Often these files are edited by hand and around the values you want you get unwanted character codes (e.g. tabs and spaces) . You can tidy it up easily:

pairs[x.strip()] = y.strip()

That might save a few hours bug-hunting.
Grim
__________________
*** Experimental Python Markup CGI V2 ***

Reply With Quote
  #4  
Old February 24th, 2004, 03:02 PM
netytan's Avatar
netytan netytan is offline
Hello World :)
Dev Shed Frequenter (2500 - 2999 posts)
 
Join Date: Mar 2003
Location: Hull, UK
Posts: 2,537 netytan User rank is Second Lieutenant (5000 - 10000 Reputation Level)netytan User rank is Second Lieutenant (5000 - 10000 Reputation Level)netytan User rank is Second Lieutenant (5000 - 10000 Reputation Level)netytan User rank is Second Lieutenant (5000 - 10000 Reputation Level)netytan User rank is Second Lieutenant (5000 - 10000 Reputation Level)netytan User rank is Second Lieutenant (5000 - 10000 Reputation Level)netytan User rank is Second Lieutenant (5000 - 10000 Reputation Level) 
Time spent in forums: 1 Week 2 Days 18 h 17 m 47 sec
Reputation Power: 68
Send a message via ICQ to netytan Send a message via AIM to netytan Send a message via MSN to netytan Send a message via Yahoo to netytan
Just another example, based on sfb's with a few minor differences. Mainly to show the "if 'key' in dict" statment and stop unpacking errors.

1. only splitting on the first colon, this helps stop unpacking errors resulting from lines containing more than one colon.
2. uses if statments instead of try-except blocks. Just a personal preferance

Code:
#!/usr/bin/env python

parts = {}

for line in file('file.txt'):
    try:
        x, y = line.split(':', 1)
        parts[x] = y
    except:
        pass
     
value = raw_input('Enter a value')

if value in parts:
    print value, 'links to', parts[value]
else:
    print value, 'not found in parts'


Note: this does requite Python 2.3.

Mark.
__________________
programming language development: www.netytan.com Hula


Last edited by netytan : February 24th, 2004 at 03:14 PM.

Reply With Quote
Reply

Viewing: Dev Shed ForumsProgramming LanguagesPython Programming > Accessing text file

Developer Shed Advertisers and Affiliates



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

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


Powered by: vBulletin Version 3.0.5
Copyright ©2000 - 2013, Jelsoft Enterprises Ltd.

© 2003-2013 by Developer Shed. All rights reserved. DS Cluster - Follow our Sitemap