Reading content of file into variable and splitting into an array

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • neeru29
    New Member
    • Feb 2009
    • 21

    #1

    Reading content of file into variable and splitting into an array

    hello,

    i'm trying to read the content of file into the variable at once. It has been successful. But how can i split each new line into array.

    Let us assume the file contains the following line
    192.114.82.90 - - [01/Nov/1998:00:32:54 +0200] www.actcom.co.il "GET /~choo/lupg/images/good_bar.gif HTTP/1.0" 404 -
    192.114.82.90 - - [01/Nov/1998:00:33:03 +0200] www.actcom.co.il "GET /~choo/lupg/images/good_bar.gif HTTP/1.0" 300 1234
    192.114.82.90 - - [01/Nov/1998:00:33:03 +0200] www.actcom.co.il "GET /~choo/lupg/images/good_bar.gif HTTP/1.0" 300 -

    Code:
    def file_get_contents(filename):
    	with open(filename) as a_file:
    		return a_file.read()
    
    content = file_get_contents(filename)
    Thanks in advance....
  • bvdet
    Recognized Expert Specialist
    • Oct 2006
    • 2851

    #2
    File method readlines() will read the entire file and return a list of the lines which is the equivalent of:
    Code:
    f = open("file_name")
    lineList = []
    for line in f:
        lineList.append(line)

    Comment

    Working...