| | |
Searching a text file
Please support our Python advertiser: Programming Forums - DaniWeb Sister Site
![]() |
•
•
Join Date: Mar 2007
Posts: 1
Reputation:
Solved Threads: 0
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..
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..
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
--------------
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.
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
--------------
Python Syntax (Toggle Plain Text)
try: fp = open("1.txt", 'r') acronym_dictionary = dict() for line in fp.readlines(): if acronym_dictionary.has_key(line.split('-')[0]): acronym_dictionary[line.split('-')[0]].append(line.split('-')[1]) else: acronym_dictionary[line.split('-')[0]] = [ line.split('-')[1] ] fp.close() acronym = raw_input("Enter an acronym") if acronym_dictionary.has_key(acronym): for expansion in acronym_dictionary[acronym]: print expansion except IOError, e: print e except IndexError: 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 1:43 am.
![]() |
Similar Threads
- connect to text file database (Visual Basic 4 / 5 / 6)
- Storing Text file in Database through VB (Visual Basic 4 / 5 / 6)
- Search string in a text file (C)
- New User (C)
- Help with delphi project (Pascal and Delphi)
- Store Bluetooth remote address to a text file (C++)
- 10 line text file (Java)
- Read and write to an ASCII Text file (Java)
Other Threads in the Python Forum
- Previous Thread: wxPython Events!!!!!!!!????
- Next Thread: Trouble with module-scope variables
Views: 2451 | Replies: 1
| Thread Tools | Search this Thread |
Tag cloud for Python
approximation array beginner book builtin change cipher clear client code color converter countpasswordentry cturtle curved def dictionary drive dynamic examples excel file float format ftp function gui homework import inches input java library line lines linux list lists loop microcontroller mouse mysqldb mysqlquery newb number numbers output parsing path plugin port prime program programming projects py2exe pygame pymailer pyqt python random recursion recursive redirect remote script scrolledtext search singleton socket sqlite ssh string strings strip subprocess sum syntax table terminal text textarea thread threading time tkinter tlapse tuple tutorial twoup ubuntu unicode urllib urllib2 variable vigenere wikipedia windows wxpython xlwt





