473,809 Members | 2,781 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Parsing a txt file

I would need a "script" that parses the following information in
'outdoor.txt'.
What i actually need are those variables (for example $yTemp). I'd do it
myself but my knowhow lacks ;D
outdoor.txt is input and variables are "output" which i need to make graphs
(jgraph)

-------OUTDOOR.txt-------
Date; Time; Temp øC
21.11.2003; 18:21:39; -2.69
24.12.2003; 23:10:45; 0.06
01.01.2004; 00:00:29; -5.75
01.01.2004; 00:43:37; -6.13
03.01.2004; 22:13:38; -8.44
05.01.2004; 07:38:58; -4.06
05.01.2004; 09:33:19; -4.50
-------OUTDOOR.txt-------

i know how to read the last line and get three variables:
....
$in_part = explode(";", $lastRow);
$in_pvmOUT = $in_part[0]; // Date = 05.01.2004
$in_aikaOUT = $in_part[1]; // Aika = 09:33:19
$in_lampoOUT = $in_part[2]; // Temp øC = -4.50

But i don't know how to get more values at once and feed it to the array:

Values should be in the following format:
$yTemp = array(-2.69, 0.06, -5.78); // Average temperatures for each month
$xAverage = array("Nov 2003","Dec 2003","Jan 2004");

It actually has values for every minute and every day.
So how should it be done if i want to draw a graph for "today", say
5.1.2004.
$yTempToday =
array(-4.38, -4.44, -4.44, -4.44, -4.44, -4.44, -4.44, -4.50, -4.50, -4.50,
-4.50, -4.44, -4.44, -4.50);
$xTimeDecimal = array(9.40, 9.42, 9.43, 9.45, 9.47, 9.48, 9.50, 9.52, 9.54,
9.56, 9.58, 9.59, 9.61, 9.63);
or $xTimeActual = array("09:23:37 ", "09:24:42", "09:25:46", "09:26:51",
"09:27:56", "09:29:00", "09:30:05", "09:31:10", "09:32:15", "09:33:19",
"09:34:24", "09:35:29", "09:36:34", "09:37:39") ;
-------OUTDOOR.txt-------
Date; Time; Temp øC
21.11.2003; 18:21:39; -2.69
24.12.2003; 23:10:45; 0.06
01.01.2004; 00:00:29; -5.75
01.01.2004; 00:43:37; -6.13
03.01.2004; 22:13:38; -8.44
05.01.2004; 09:23:37; -4.38
05.01.2004; 09:24:42; -4.44
05.01.2004; 09:25:46; -4.44
05.01.2004; 09:26:51; -4.44
05.01.2004; 09:27:56; -4.44
05.01.2004; 09:29:00; -4.44
05.01.2004; 09:30:05; -4.44
05.01.2004; 09:31:10; -4.50
05.01.2004; 09:32:15; -4.50
05.01.2004; 09:33:19; -4.50
05.01.2004; 09:34:24; -4.50
05.01.2004; 09:35:29; -4.44
05.01.2004; 09:36:34; -4.44
05.01.2004; 09:37:39; -4.50
-------OUTDOOR.txt-------
Jul 17 '05 #1
2 11362
Jari wrote:
I would need a "script" that parses the following information in
'outdoor.txt'.
...
-------OUTDOOR.txt-------
Date; Time; Temp øC
21.11.2003; 18:21:39; -2.69
24.12.2003; 23:10:45; 0.06
01.01.2004; 00:00:29; -5.75
01.01.2004; 00:43:37; -6.13
03.01.2004; 22:13:38; -8.44
05.01.2004; 07:38:58; -4.06
05.01.2004; 09:33:19; -4.50
-------OUTDOOR.txt-------
...


You can use the CSV function:
http://www.php.net/manual/en/function.fgetcsv.php
Jul 17 '05 #2
"Jari" <no*@here.fi> schreef in bericht news:bt******** **@news.cc.tut. fi...
I would need a "script" that parses the following information in
'outdoor.txt'.
-------OUTDOOR.txt-------
Date; Time; Temp øC
21.11.2003; 18:21:39; -2.69
24.12.2003; 23:10:45; 0.06
01.01.2004; 00:00:29; -5.75
01.01.2004; 00:43:37; -6.13
03.01.2004; 22:13:38; -8.44
05.01.2004; 07:38:58; -4.06
05.01.2004; 09:33:19; -4.50
-------OUTDOOR.txt-------
Values should be in the following format:
$yTemp = array(-2.69, 0.06, -5.78); // Average temperatures for each month
$xAverage = array("Nov 2003","Dec 2003","Jan 2004");
So how should it be done if i want to draw a graph for "today", say
5.1.2004.
$yTempToday =
array(-4.38, -4.44, -4.44, -4.44, -4.44, -4.44, -4.44, -4.50, -4.50, -4.50, -4.50, -4.44, -4.44, -4.50);
$xTimeDecimal = array(9.40, 9.42, 9.43, 9.45, 9.47, 9.48, 9.50, 9.52, 9.54, 9.56, 9.58, 9.59, 9.61, 9.63);
or $xTimeActual = array("09:23:37 ", "09:24:42", "09:25:46", "09:26:51",
"09:27:56", "09:29:00", "09:30:05", "09:31:10", "09:32:15", "09:33:19",
"09:34:24", "09:35:29", "09:36:34", "09:37:39") ;


Try the code below. If you have difficulties understanding what the code
does, look up what the functions array(), file(), count(), number_format() ,
date(), trim(), array_sum(), strtotime() and empty() do in the PHP manual at
www.php.net or post any questions you have. I had wanted to put comments
everywhere but wasn't sure that you would want that. I assumed you know how
global variables are changed in functions, if not, just ask me how to use
this code.
Note that this code doesn't check the input very much, for example it simply
assumes that it should skip the first line of OUTDOOR.txt. The safest code
is of course longer, or it would use more advanced input processing
techniques like regular expressions. But I figured the code below would
provide a working and understandable solution.

Remon Huijts
<?php
function per_month() {
global $yTemp, $xLabel;
$yTemp = array();
$xLabel = array();
$lines = file('OUTDOOR.t xt');
$temps = array();
$year = 0;
$month = 0;

for ($i = 1; $i < count($lines); $i++) {
$this_line = explode(';', trim($lines[$i]));
$date = explode('.', trim($this_line[0]));
$time = explode(':', trim($this_line[1]));

if ($date[2] > $year || $date[1] > $month) {
if (count($temps) > 0) {
$yTemp[] = number_format(a rray_sum($temps ) / count($temps),
2);
$xLabel[] = date('M Y', strtotime("$yea r-$month-01"));
}
$temps = array();
$month = $date[1];
$year = $date[2];
}
$temps[] = trim($this_line[2]);
}
if (count($temps) > 0) {
$yTemp[] = number_format(a rray_sum($temps ) / count($temps), 2);
$xLabel[] = date('M Y', strtotime("$yea r-$month-01"));
}
}

function per_day($day = '') {
global $yTemp, $xLabel, $xValue;
// $day is like '05.01.2004' WITH leading zero, or empty for today
if (empty($day)) $day = date('d.m.Y');
$yTemp = array();
$xLabel = array();
$xValue = array();
$lines = file('OUTDOOR.t xt');

for ($i = 1; $i < count($lines); $i++) {
$this_line = explode(';', trim($lines[$i]));
if (trim($this_lin e[0]) == $day) {
$time = explode(':', trim($this_line[1]));
$minutes = (float)$time[1] + (float)$time[2] / 60;

$yTemp[] = trim($this_line[2]);
$xLabel[] = trim($this_line[1]);
$xValue[] = (integer)$time[0] + '.' + number_format(( $minutes /
60), 2);
}
}
}
?>
Jul 17 '05 #3

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

3
3663
by: Willem Ligtenberg | last post by:
I decided to use SAX to parse my xml file. But the parser crashes on: File "/usr/lib/python2.3/site-packages/_xmlplus/sax/handler.py", line 38, in fatalError raise exception xml.sax._exceptions.SAXParseException: NCBI_Entrezgene.dtd:8:0: error in processing external entity reference This is caused by: <!DOCTYPE Entrezgene-Set PUBLIC "-//NCBI//NCBI Entrezgene/EN" "NCBI_Entrezgene.dtd">
2
3962
by: Cigdem | last post by:
Hello, I am trying to parse the XML files that the user selects(XML files are on anoher OS400 system called "wkdis3"). But i am permenantly getting that error: Directory0: \\wkdis3\ROOT\home Canonicalpath-Directory4: \\wkdis3\ROOT\home\bwe\ You selected the file named AAA.XML getXmlAlgorithmDocument(): IOException Not logged in
3
3510
by: Pir8 | last post by:
I have a complex xml file, which contains stories within a magazine. The structure of the xml file is as follows: <?xml version="1.0" encoding="ISO-8859-1" ?> <magazine> <story> <story_id>112233</story_id> <pub_name>Puleen's Publication</pub_name> <pub_code>PP</pub_code> <edition_date>20031201</edition_date>
1
2466
by: Christoph Bisping | last post by:
Hello! Maybe someone is able to give me a little hint on this: I've written a vb.net app which is mainly an interpreter for specialized CAD/CAM files. These files mainly contain simple movement and drawing instructions like "move to's" and "change color's" optionally followed by one or more numeric (int or float) arguments. My problem is that the parsing algorithm I've currently implemented is extremely slow.
4
4868
by: Rick Walsh | last post by:
I have an HTML table in the following format: <table> <tr><td>Header 1</td><td>Header 2</td></tr> <tr><td>1</td><td>2</td></tr> <tr><td>3</td><td>4</td></tr> <tr><td>5</td><td>6</td></tr> </table> With an XSLT styles sheet, I can use for-each to grab the values in
3
4390
by: toton | last post by:
Hi, I have some ascii files, which are having some formatted text. I want to read some section only from the total file. For that what I am doing is indexing the sections (denoted by .START in the file) with the location. And for a particular section I parse only that section. The file is something like, .... DATAS
9
1992
by: Paulers | last post by:
Hello, I have a log file that contains many multi-line messages. What is the best approach to take for extracting data out of each message and populating object properties to be stored in an ArrayList? I have tried looping through the logfile using regex, if statements and flags to find the start and end of each message but I do not see a good time in this process to create a new instance of my Message object. While messing around with...
13
4529
by: Chris Carlen | last post by:
Hi: Having completed enough serial driver code for a TMS320F2812 microcontroller to talk to a terminal, I am now trying different approaches to command interpretation. I have a very simple command set consisting of several single letter commands which take no arguments. A few additional single letter commands take arguments:
13
2835
by: charliefortune | last post by:
I am fetching some product feeds with PHP like this $merch = substr($key,1); $feed = file_get_contents($_POST); $fp = fopen("./feeds/feed".$merch.".txt","w+"); fwrite ($fp,$feed); fclose ($fp); and then parsing them with PHP's native parsing functions. This is succesful for most of the feeds, but a couple of them claim to be
2
3620
by: Felipe De Bene | last post by:
I'm having problems parsing an HTML file with the following syntax : <TABLE cellspacing=0 cellpadding=0 ALIGN=CENTER BORDER=1 width='100%'> <TH BGCOLOR='#c0c0c0' Width='3%'>User ID</TH> <TH Width='10%' BGCOLOR='#c0c0c0'>Name</TH><TH width='7%' BGCOLOR='#c0c0c0'>Date</TH> and so on.... whenever I feed the parser with such file I get the error :
0
9721
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
9602
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10639
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
10376
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
10383
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
5550
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
5688
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
4332
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
3861
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.