Recalling Lists

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • texas22
    New Member
    • Jun 2007
    • 26

    Recalling Lists

    If you have a text file that say has a list of 1000 plus words is there a way that you can recall that file without having to type the whole list out word by word and which will also allow you to once recalled sort the list and do other things to it
  • ilikepython
    Recognized Expert Contributor
    • Feb 2007
    • 844

    #2
    Originally posted by texas22
    If you have a text file that say has a list of 1000 plus words is there a way that you can recall that file without having to type the whole list out word by word and which will also allow you to once recalled sort the list and do other things to it
    What do you mean? Is it something like this?:

    text.txt
    Code:
    word1, word2, word3 ...,
    word4, word5, word6 ...
    [code=python]
    inFile = file("test.txt" )
    wordlist = [word.strip() for word in inFile.read().s plit(",")]
    wordlist.sort()
    [/code]

    Comment

    • bartonc
      Recognized Expert Expert
      • Sep 2006
      • 6478

      #3
      Originally posted by texas22
      If you have a text file that say has a list of 1000 plus words is there a way that you can recall that file without having to type the whole list out word by word and which will also allow you to once recalled sort the list and do other things to it
      Yep. Presuming that each word is on its own line followed by some sort of line-terminator (new-line):[CODE=python]fileName = r"C:\this\is\a\ path\theFile.tx t"
      # I just make up variable names, you can use you own
      inputFile = open(fileName)
      wordList = inputFile.readl ines()
      inputFile.close ()
      wordList.sort()

      # Writing is just as easy, just open a file in (w)rite mode
      fileName = "whatever"
      outputFile = open(fileName, "w")
      outputFile.writ elines(wordList )
      outputFile.clos e()[/CODE]
      Which would, of course, keep the new-line chrs with each word.

      Comment

      Working...