User Name Password Register
DaniWeb IT Discussion Community
All
What is DaniWeb IT Discussion Community?
You're currently browsing the Python section within the Software Development category of DaniWeb, a massive community of 392,081 software developers, web developers, Internet marketers, and tech gurus who are all enthusiastic about making contacts, networking, and learning from each other. In fact, there are 3,968 IT professionals currently interacting right now! Registration is free, only takes a minute and lets you enjoy all of the interactive features of the site.
Please support our Python advertiser:
Views: 1142 | Replies: 1
Reply
Join Date: Mar 2007
Posts: 1
Reputation: emzy is an unknown quantity at this point 
Rep Power: 0
Solved Threads: 0
emzy emzy is offline Offline
Newbie Poster

Searching a text file

  #1  
Mar 28th, 2007
Hi All,

I'm having huge problems with a program that i am attempting to write. What i have is a program that prints Acronyms and sentences to a text file already, now i am trying to create a new program that reads that text file and if the user inputs the same acronym, it will print out the sentence associated with that acronym. This is what i have so far;

import string

def main():

acronymName = raw_input("Enter a sentence that may include more than one acronym ")
fileHandle = open ( 'acronym.txt', 'r' )

for line in fileHandle.readlines():
for acronym in line.split():
if acronym.enswith('nce'):
print acronym


fileHandle.close()

main()


Obviously Im not even sure if i am on the right track. Any help would be appreciated. I can't even seem to find a similar program to compare to.

Thanks Heaps..
AddThis Social Bookmark Button
Reply With Quote  
Join Date: Jan 2006
Posts: 215
Reputation: katharnakh is an unknown quantity at this point 
Rep Power: 3
Solved Threads: 19
katharnakh's Avatar
katharnakh katharnakh is offline Offline
Posting Whiz in Training

Re: Searching a text file

  #2  
Mar 29th, 2007
Hi, from next time onwards you need to place your code between 'Code' tag.
for eg: I will code in reverse order to refrain its affect,
[/code]
your code goes here...
[Code = Python]

I have assumed the input file like following,

accronym.txt
--------------
IIMB-Indian Institute of Management, Bangalore
ASCII-American Standard Code for Information Interchange
PERL-Practical Extraction and Reporting Language
ANSI-American National Standards Institute
ANSI-Advanced Neuromodulation Systems, Inc
--------------
  1. try:
  2. fp = open("1.txt", 'r')
  3.  
  4. acronym_dictionary = dict()
  5. for line in fp.readlines():
  6. if acronym_dictionary.has_key(line.split('-')[0]):
  7. acronym_dictionary[line.split('-')[0]].append(line.split('-')[1])
  8. else:
  9. acronym_dictionary[line.split('-')[0]] = [ line.split('-')[1] ]
  10.  
  11. fp.close()
  12.  
  13. acronym = raw_input("Enter an acronym")
  14. if acronym_dictionary.has_key(acronym):
  15. for expansion in acronym_dictionary[acronym]:
  16. print expansion
  17. except IOError, e:
  18. print e
  19. except IndexError:
  20. pass

I have used dictionary(or hash) to keep the details. Acronym is key and its expansion is its value. I have used list to keep expansions. Because if an acronym has more than one expansion then it does not violate the dictionary rule. That is key should be unique in dictionary.
hope this helps...

kath.
Last edited by katharnakh : Mar 29th, 2007 at 12:43 am.
challenge the limits
Reply With Quote  
Reply

Only community members can participate in forum threads. You must register or log in to contribute.

Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)

 

DaniWeb Python Marketplace
Thread Tools Display Modes

Similar Threads
Other Threads in the Python Forum

All times are GMT -4. The time now is 12:23 pm.
Forum system based on vBulletin Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
©2003 - 2008 DaniWeb® LLC