Thread: Starting Python
View Single Post
Join Date: Oct 2004
Posts: 3,856
Reputation: vegaseat is just really nice vegaseat is just really nice vegaseat is just really nice vegaseat is just really nice vegaseat is just really nice 
Solved Threads: 866
Moderator
vegaseat's Avatar
vegaseat vegaseat is offline Offline
DaniWeb's Hypocrite

Re: Starting Python

 
0
  #6
Mar 23rd, 2005
One more helpful hint to get this thing off to a hopefully good start. How do we read a simple text file in Python? Also, what can we do with the data after we read it?
  1. # read a text file to a string and create a list of words
  2.  
  3. # use any text file you have ...
  4. textf = open('xmas.txt', 'r')
  5. str1 = textf.read()
  6. textf.close()
  7.  
  8. print( "The text file as one string:" )
  9. print( str1 )
  10.  
  11. # splits at the usual whitespaces
  12. wordlist = str1.split(None)
  13. print( "\nThe string as a list of words:" )
  14. print( wordlist )
  15.  
  16. print( "\nThere are %d words in the list." % len(wordlist) )
Want more help about split()? At the interactive page >>> prompt enter
help("string.split")
Last edited by vegaseat; Aug 16th, 2009 at 10:14 pm. Reason: [code=python], Python3 update
May 'the Google' be with you!
Reply With Quote