943,704 Members | Top Members by Rank

Ad:
  • Python Discussion Thread
  • Marked Solved
  • Views: 661
  • Python RSS
Sep 21st, 2008
0

importing files to lists

Expand Post »
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:
Python Syntax (Toggle Plain Text)
  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()
Similar Threads
Reputation Points: 34
Solved Threads: 17
Junior Poster
predator78 is offline Offline
168 posts
since Sep 2008
Sep 21st, 2008
0

Re: importing files to lists

Something like this will do:
python Syntax (Toggle Plain Text)
  1. for line in wordlistfile:
  2. wordlist.append(line.strip())
Reputation Points: 404
Solved Threads: 180
Nearly a Posting Virtuoso
bumsfeld is offline Offline
1,422 posts
since Jul 2005
Sep 21st, 2008
0

Re: importing files to lists

Thanks works like a charm. What exactly does strip() do though? Just wondering.
Reputation Points: 34
Solved Threads: 17
Junior Poster
predator78 is offline Offline
168 posts
since Sep 2008
Sep 22nd, 2008
0

Re: importing files to lists

Reputation Points: 930
Solved Threads: 667
Posting Maven
Gribouillis is offline Offline
2,655 posts
since Jul 2008
Sep 22nd, 2008
0

Re: importing files to lists

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.
Reputation Points: 34
Solved Threads: 17
Junior Poster
predator78 is offline Offline
168 posts
since Sep 2008

This thread is solved

Either the thread starter or a moderator has marked this thread as solved. You can most likely trust the responses and answers given. There is most likely no reason for any further responses to be posted here. If you have a related question, please start a new thread in this forum instead.

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in Python Forum Timeline: Deploying a Python Service on Axis2
Next Thread in Python Forum Timeline: part 2->How to find which menu option is selected





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC