954,541 Members — Technology Publication meets Social Media
Username:
Password:
Lost login information?
Have something to say? Contribute New Article Reply to this Article

importing files to lists

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:

#decode scrambled word from a word list in 30 sec or less.
wordlist = []
possible = []
matches = []
wordlist = []
i = -1
p = 0
wordlistfile = open('C:\\Documents and Settings\\William\\Desktop\\wordlist\\wordlist.txt', 'r')
possiblefile = open('C:\\Documents and Settings\\William\\Desktop\\possible.txt', 'r')

#add file contents to lists.
for line in wordlistfile:
    wordlist.append(line)
for line in possiblefile:
    possible.append(line)

for line in wordlist:
    i = i + 1
    if len(wordlist[i]) == len(possible[p]):
        matches.append(wordlist[i])
print matches

        
    

wordlistfile.close()
possiblefile.close()
predator78
Junior Poster
168 posts since Sep 2008
Reputation Points: 34
Solved Threads: 17
 

Something like this will do:

for line in wordlistfile:
    wordlist.append(line.strip())
bumsfeld
Nearly a Posting Virtuoso
1,445 posts since Jul 2005
Reputation Points: 404
Solved Threads: 184
 

Thanks works like a charm. What exactly does strip() do though? Just wondering.

predator78
Junior Poster
168 posts since Sep 2008
Reputation Points: 34
Solved Threads: 17
 
Gribouillis
Posting Maven
Moderator
2,786 posts since Jul 2008
Reputation Points: 1,044
Solved Threads: 691
 

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.;)

predator78
Junior Poster
168 posts since Sep 2008
Reputation Points: 34
Solved Threads: 17
 

This question has already been solved

Post: Markdown Syntax: Formatting Help
You