473,387 Members | 1,529 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,387 software developers and data experts.

Reading content of file into variable and splitting into an array

21
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 -

Expand|Select|Wrap|Line Numbers
  1. def file_get_contents(filename):
  2.     with open(filename) as a_file:
  3.         return a_file.read()
  4.  
  5. content = file_get_contents(filename)
  6.  
Thanks in advance....
Aug 25 '10 #1
1 5646
bvdet
2,851 Expert Mod 2GB
File method readlines() will read the entire file and return a list of the lines which is the equivalent of:
Expand|Select|Wrap|Line Numbers
  1. f = open("file_name")
  2. lineList = []
  3. for line in f:
  4.     lineList.append(line)
Aug 26 '10 #2

Sign in to post your reply or Sign up for a free account.

Similar topics

14
by: Kevin Knorpp | last post by:
Hello. I need to be able to extract the data from the attached file (or any file in the same format) so that I can work with the data in PHP. I'm fairly comfortable with using PHP with...
3
by: deko | last post by:
It's nice to be able to generate an html table from a PHP array. I know how to do this, but the array in question is built from a file. The file in question can be very long, and I only want the...
9
by: dr. zoidberg | last post by:
Is it possible to put Variable in Array? Here is what I need: $array = Array($a,$b); $a = 'something'; $b = 'something else'; foreach ($array as $val) { echo "$val"; }
4
by: somaboy mx | last post by:
hi, I'm on winXPpro / apache 1.2 / php 4.4.x I'm experimenting with writing and reading from textfiles via php. I can create a file with fopen, write to it, but I'm having trouble reading...
2
by: Anders Eriksson | last post by:
Hello! I'm a beginner at PHP and I wonder how I would go about to read a textfile into an array(hash). The textfile looks like this: A/S=age/sex? A/S/L=age/sex/location? AA=alcoholics...
8
by: dbuser | last post by:
Hi, I need help on a problem, as described below. I am reading a file "input.txt"which has data like this: abc def gh izk lmnopq rst uvwxyz I am using fstream object to read the file and...
7
by: eriwik | last post by:
Hi, I'm working on a small application which processes PNG-images and need to read parts of them into structures and/or variables and I was wondering what assumptions one can make about how the...
11
by: Freddy Coal | last post by:
Hi, I'm trying to read a binary file of 2411 Bytes, I would like load all the file in a String. I make this function for make that: '-------------------------- Public Shared Function...
5
by: jonathan184 | last post by:
Hi I am trying to access a log file and read the fikle into an array and split the file using the space as a delimiter but i get this error. Parse error: syntax error, unexpected $end in...
2
by: joeschnell | last post by:
The compiler is taking my code here // stocks.cpp : Defines the entry point for the console application. // //#include "stdafx.h" #include <iostream> #include <fstream> #include <string>
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.