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:
  #1  
Old November 9th, 2004, 07:08 PM
Zopyrus Zopyrus is offline
Registered User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Oct 2004
Location: Sweden
Posts: 20 Zopyrus User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 6 h 13 m 28 sec
Reputation Power: 0
Need help on PythoN please! :)

Hello folks!

I need some help once more, thank you.

This time I want to do a "Searching" code.

Here is an FILE called "Ordlista.txt" with 1862 Words that contains only 5 letters.Click here to see Ordlista

I want to make a code, i.e. create a "kolla1.py" that will at the start of the Terminalconsole will get a word from the user. Then the program will ask the user what list to check (Ordlista) and he will have to write in ordlista.txt

The program shall then find the word the user asked about from the beginning it shall seem something like this.

:~> python Kolla1.py trams
Write name of the file to search in: ordlista.txt
Wordlist read!
The word trams is on the list.

Can anyone help me out? About how i shall begin ? :S

Thanks,

Reply With Quote
  #2  
Old November 9th, 2004, 07:54 PM
Boki's Avatar
Boki Boki is offline
A wanna-be guru of some sort
Dev Shed Novice (500 - 999 posts)
 
Join Date: Sep 2004
Location: Either online or offline
Posts: 624 Boki User rank is Sergeant (500 - 2000 Reputation Level)Boki User rank is Sergeant (500 - 2000 Reputation Level)Boki User rank is Sergeant (500 - 2000 Reputation Level)Boki User rank is Sergeant (500 - 2000 Reputation Level)Boki User rank is Sergeant (500 - 2000 Reputation Level) 
Time spent in forums: 2 Days 4 h 7 m 13 sec
Reputation Power: 14
First you need to use sys.argv to get the wor dto be searched for, that the user inputs in the command line as the argument to your .py file. Then you can use raw_input() to get the name of the file. You open that file, read ines, nd then you can create a list or an array of strings of all words in the file. Then you can say, if the word he user input is in the list or the array, rint word found, else print word not found.

It'd go something like that... One could do a better job, but this is just from at the top of my head right now...

Reply With Quote
  #3  
Old November 10th, 2004, 06:11 AM
Zopyrus Zopyrus is offline
Registered User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Oct 2004
Location: Sweden
Posts: 20 Zopyrus User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 6 h 13 m 28 sec
Reputation Power: 0
Hey dude,

I know what you mean this is what i got so far but it doesn't work since I haven't completed it, but I dont know what I should do now? Need help please.

Code:
#coding: iso-8859-1
from string import *
a=raw_input("Skriv in namnet på filen: ")
text=open(a)
list=[]
for n in text:
    list=list.append(n)
print list
text.close()

Reply With Quote
  #4  
Old November 11th, 2004, 02:28 PM
sfb sfb is offline
Contributing User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Nov 2003
Posts: 447 sfb User rank is Corporal (100 - 500 Reputation Level)sfb User rank is Corporal (100 - 500 Reputation Level)sfb User rank is Corporal (100 - 500 Reputation Level)sfb User rank is Corporal (100 - 500 Reputation Level) 
Time spent in forums: 2 Days 1 h 43 m 45 sec
Reputation Power: 10
Code:
list=[]


There is a builtin function called "list", and with that code you are overwriting it, which isn't good because it can cause problems elsewhere in the program...

Code:
for n in text:
    list=list.append(n)


An open file has the "readlines()" method to get all the lines in one go and return a list to save you building the list yourself.

Lastly, one of the great if tests in Python is:

if item in sequence:

and it will search the list/string/tuple/etc for you, so what you could do is something like this:

Code:
word = raw_input("Please enter a word: ")
filename = raw_input("Please enter the file to search: ")

data = open(filename).readlines()

if word in data:
    print "Word found!"
else:
    print "word not found :("



Reply With Quote
Reply

Viewing: Dev Shed ForumsProgramming LanguagesPython Programming > Need help on PythoN please! :)


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 | 
  
 





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