importing files to lists

Please support our Python advertiser: Programming Forums - DaniWeb Sister Site
Thread Solved

Join Date: Sep 2008
Posts: 20
Reputation: predator78 is an unknown quantity at this point 
Solved Threads: 0
predator78 predator78 is offline Offline
Newbie Poster

importing files to lists

 
0
  #1
Sep 21st, 2008
Hey guys,
I been working on a small project I found on another forum. It's supposed to decode scrambled word from a word list in 30 sec or less. Well I'm pretty noobish so my method might not be all that great, but I'm giving it a go and learning quite a bit along the way. Anyways I figured out how to get the information from the files and put it into lists, but the problem I'm facing now is that the copied lists carry over the \n with them, which I need to get rid of. Any ideas or suggestions would be awsome thanks.

here is the code:
  1. #decode scrambled word from a word list in 30 sec or less.
  2. wordlist = []
  3. possible = []
  4. matches = []
  5. wordlist = []
  6. i = -1
  7. p = 0
  8. wordlistfile = open('C:\\Documents and Settings\\William\\Desktop\\wordlist\\wordlist.txt', 'r')
  9. possiblefile = open('C:\\Documents and Settings\\William\\Desktop\\possible.txt', 'r')
  10.  
  11. #add file contents to lists.
  12. for line in wordlistfile:
  13. wordlist.append(line)
  14. for line in possiblefile:
  15. possible.append(line)
  16.  
  17. for line in wordlist:
  18. i = i + 1
  19. if len(wordlist[i]) == len(possible[p]):
  20. matches.append(wordlist[i])
  21. print matches
  22.  
  23.  
  24.  
  25.  
  26. wordlistfile.close()
  27. possiblefile.close()
Reply With Quote Quick reply to this message  
Join Date: Jul 2005
Posts: 1,221
Reputation: bumsfeld will become famous soon enough bumsfeld will become famous soon enough 
Solved Threads: 137
bumsfeld's Avatar
bumsfeld bumsfeld is offline Offline
Nearly a Posting Virtuoso

Re: importing files to lists

 
0
  #2
Sep 21st, 2008
Something like this will do:
  1. for line in wordlistfile:
  2. wordlist.append(line.strip())
Should you find Irony, you can keep her!
Reply With Quote Quick reply to this message  
Join Date: Sep 2008
Posts: 20
Reputation: predator78 is an unknown quantity at this point 
Solved Threads: 0
predator78 predator78 is offline Offline
Newbie Poster

Re: importing files to lists

 
0
  #3
Sep 21st, 2008
Thanks works like a charm. What exactly does strip() do though? Just wondering.
Reply With Quote Quick reply to this message  
Join Date: Jul 2008
Posts: 939
Reputation: Gribouillis is a jewel in the rough Gribouillis is a jewel in the rough Gribouillis is a jewel in the rough 
Solved Threads: 217
Gribouillis's Avatar
Gribouillis Gribouillis is online now Online
Posting Shark

Re: importing files to lists

 
0
  #4
Sep 22nd, 2008
Reply With Quote Quick reply to this message  
Join Date: Sep 2008
Posts: 20
Reputation: predator78 is an unknown quantity at this point 
Solved Threads: 0
predator78 predator78 is offline Offline
Newbie Poster

Re: importing files to lists

 
0
  #5
Sep 22nd, 2008
I see thanks, that is one of the few items in the library that I can actually understand, lol. I'm not very good with the terminology so it always sounds so cryptic in the definitions.
Reply With Quote Quick reply to this message  
Reply

This thread has been marked solved.
Perhaps start a new thread instead?
Message:


Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC