I would like to check if csv file contain a header and ignore the header.
I have to do a check if the first column is not a character
csv file has format : avgTemperature, minTemperature, maxTemperature
$f = fopen("./uploads/" .$filename, "r");
$string = "avgTemperature";
if (fgetcsv($f)==$string){
// read the first line and ignore it
fgets($f);
}
I assume your complete code uses a loop (while or for).
As such, you have a few options.
Simply skip the first row always.
Use logic to test for the header row then skip.
Either way, continue is the key piece.
PHP pseudo code:
while (…) {
if ($row == $header_row) {
continue;
}
// data rows
}
UPDATE
The logic for determining if the first row is a header row seems like a better solution in your case. You could use the following to test for that.
if ($row[0] == 'avgTemperature') {
// header row
}
Note: This makes the assumption that the first column of data is avgTemperature and it's header is avgTemperature. Adjust as necessary.
Going from your comment, and from the idea that the actual data is temperatures (i.e. numeric data), if you do have headers, then they will be text strings and not numbers. Therefore you can do something like this:
$f = fopen("./uploads/" .$filename, "r");
if(!($data = fgetcsv($f))) {
return; //most likely empty file
}
if(!is_numeric($data[0])) {
//this is your header line - skip it - and read the next line
$data = fgetcsv($f);
}
while($data) {
//process a line of data
...
//and read the next line
$data = fgetcsv($f);
}
EDIT: An alternative version of the last loop would look like this:
do {
//process a line of data
...
}
while ($data = fgetcsv($f));
Related
I am trying to read data from a plain text file from an industrial machine recipe. The file is generated automatically by the tool. I want to access a specific parameter in a specific section of the file.
The parameter is called "LightSrcRef_NominalGL" The problem is that there are some number of parameters named as such in the file. I specifically want the first one, and only the one, that occurs after the tag "[Scan2d]"
Note that the parameter I need does not always show on the same line number and that [Scan2d] does not always show up in the same place, but I need the parameter in the Scan2d section. It also appears that the LightSrcRef_NominalGL parameter is not always the same number of lines after [Scan2d].
What I had hoped was to read the file line by line. When I get to [Scan2d], set a flag, then when I get to the parameter, set my variable, then get out.
This is not happening. Instead, it is taking the first LightSrcRef_NominalGL in the file.
We have similar recipe analyzers, but this is the first one with this unique recipe structure. I have looked for a way to read the file in differently, but none produce different results.
When I print the actual line, it shows that the text file is reading it line by line. I do not understand why it is not behaving as expected.
Here is example of text file. In this case it is at the end of the file. In others, there will be another section after. I had to add an extra carriage return in the text file because this was not displaying them as separate lines. They are being read in by lines because if I have it print $line, it shows exactly one line.
[Scan2d]
CameraTypeName=2D
FocusPosAboveChuck=-2.59084174217116
Mag=5
CameraName=HighMag
DifRingPos=2
Gamma=-1
LightSrcDif_ColorFilter=Gray
LightSrcDif_NominalGL=0
LightSrcRef_ColorFilter=Cyan
LightSrcRef_NominalGL=195.424629214628
$catcher = 0; //used to verify the parameter only in scan2d section
$lines = file($dir.$default_directory."/".$current_recipe_file);
foreach($lines as $line)
{ $line_count ++;
if(preg_match("/[Scan2d]\b/i", $line))
{
$catcher = $line_count; //used to only catch the parameter in the Scan2D section
}
if(preg_match("/\bLightSrcRef_NominalGL=\b/i", $line))
{
$illumination_split_temp1 = preg_split("/\=/", $line);
$recipe_illum = $illumination_split_temp1[1];
if ($catcher >0)
{print $line . " ". $catcher . "<br>";
$Tool_Ins150_Stats->Add_Recipe_Tag("Illumination Level", $recipe_illum);
$catcher= 0;
break;
}
}
}
It is taking the first LightSrcRef_NominalGL in the file, not the one after Scan2d.
If the tags you are looking for are at the start of the lines in the file this can be made even simpler. I changed what you had slightly so that when the section you are interested in is found the foreach goes to the next record.
$catcher = 0;
foreach($lines as $line) {
if(preg_match("/[Scan2d]\b/i", $line)) {
$catcher = 1;
continue;
}
if(preg_match("/\bLightSrcRef_NominalGL=\b/i", $line)) {
if (!$catcher) {
continue; // we haven't found the right section yet
}
$illumination_split_temp1 = preg_split("/\=/", $line);
$recipe_illum = $illumination_split_temp1[1];
print $line . " ". $catcher . "<br>";
$Tool_Ins150_Stats->Add_Recipe_Tag("Illumination Level", $recipe_illum);
$catcher= 0;
break;
}
}
$lines = file($dir.$default_directory."/".$current_recipe_file);
$catcher = 0; //used to verify the parameter only in scan2d section
foreach($lines as $line)
{
if(preg_match("/\[Scan2d]/", $line))
{
$catcher = 1; //used to only catch the parameter in the Scan2D section
}
if (!$catcher)
{
continue; // haven't found the right one yet, skip the rest
}
else
{
if(preg_match("/LightSrcRef_NominalGL=/", $line))
{
$illumination_split_temp1 = preg_split("/\=/", $line);
$recipe_illum = $illumination_split_temp1[1];
$Tool_Ins150_Stats->Add_Recipe_Tag("Illumination Level", $recipe_illum);
$catcher = 0;
continue;
}
}
}
This worked, but many thanks to Dave who certainly put me on the right track!! The use of Else worked when I still do not fully understand why the second preg_match was executing if $catcher was not set (and it wasn't, I printed it to be sure).
The sample your provided seems to be a file in ini format. If this is really the case, there is a very simple solution using the parse_ini_file function
<?php
$values = parse_ini_file('sample.txt', true, INI_SCANNER_TYPED);
echo "The value is " . $values["Scan2d"]["LightSrcRef_NominalGL"] . "\n";
I tried against this sample.txt file
[test]
LightSrcRef_NominalGL=0
[Scan2d]
CameraTypeName=2D
FocusPosAboveChuck=-2.59084174217116
Mag=5
CameraName=HighMag
DifRingPos=2
Gamma=-1
LightSrcDif_ColorFilter=Gray
LightSrcDif_NominalGL=0
LightSrcRef_ColorFilter=Cyan
LightSrcRef_NominalGL=195.424629214628
[test2]
LightSrcRef_NominalGL=1
And the result is:
The value is 195.42462921463
Of course, this will work only if your entire file respects the ini format as in your sample data.
My text file sample.txt. I want to exclude the first row from the text file and store the other rows into mysql database.
ID Name EMail
1 Siva xyz#gmail.com
2 vinoth xxx#gmail.com
3 ashwin yyy#gmail.com
Now I want to read this data from the text file except the first row(ID,name,email) and store into the MYsql db.Because already I have created a filed in database with the same name.
I have tried
$handle = #fopen($filename, "r"); //read line one by one
while (!feof($handle)) // Loop till end of file.
{
$buffer = fgets($handle, 4096); // Read a line.
}
print_r($buffer); // It shows all the text.
Please let me know how to do this?
Thanks.
Regards,
Siva R
It's easier if you use file() since it will get all rows in an array instead:
// Get all rows in an array (and tell file not to include the trailing new lines
$rows = file($filename, FILE_IGNORE_NEW_LINES);
// Remove the first element (first row) from the array
array_shift($rows);
// Now do what you want with the rest
foreach ($rows as $lineNumber => $row) {
// do something cool with the row data
}
If you want to get it all as a string again, without the first row, just implode it with a new line as glue:
// The rows still contain the line break, since we only trimmed the copy
$content = implode("\n", $rows);
Note: As #Don'tPanic pointed out in his comment, using file() is simple and easy but not advisable if the original file is large, since it will read the whole thing into memory as an array (and arrays take more memory than strings). He also correctly recommended the FILE_IGNORE_NEW_LINES-flag, just so you know :-)
You can just call fgets once before your while loop to get the header row out of the way.
$firstline = fgets($handle, 4096);
while (!feof($handle)) // Loop till end of file.
{ ...
I'm trying to display only the rows that contain a specific word in a specific column. Basically I would like to show only the rows that have "yes" in the Display column.
First_Name, Last_Name, Display
Kevin, Smith, yes
Jack, White, yes
Joe, Schmo, no
I've been trying various things with fgetcsv & str_getcsv from other answers and from php.net but nothing is working so far.
It doesn't do anything but this is my current code:
$csv = fopen('file.csv', 'r');
$array = fgetcsv($csv);
foreach ($array as $result) {
if ($array[2] == "yes") {
print ($result);
}
}
Let's have a look at the documentation for fgetcsv():
Gets line from file pointer and parse for CSV fields
fgetcsv reads a single line, not the whole file. You can keep reading lines until you reach the end of the file by putting it in a while loop, e.g.
<?php
$csv = fopen('file.csv', 'r');
// Keep looping as long as we get a new $row
while ($row = fgetcsv($csv)) {
if ($row[2] == "yes") {
// We can't just echo $row because it's an array
//
// Instead, let's join the fields with a comma
echo implode(',', $row);
echo "\n";
}
}
// Don't forget to close the file!
fclose($csv);
You should use data tables.
https://datatables.net/examples/basic_init/zero_configuration.html
That's how I deal with my textfiles. But be carefull, with a large amount of Data (> 10000 rows) you should have a loog at the deferRender option.
https://datatables.net/reference/option/deferRender <-- JSON DATA required.
Is it possible to validate a text file before I dump its data into a MYSQL database?
I want to check if it contains, say, 5 columns (of data). If so, then i go ahead with the following query:
LOAD DATA CONCURRENT INFILE 'c:/test/test.txt'
INTO TABLE DUMP_TABLE FIELDS TERMINATED BY '\t' ENCLOSED BY '' LINES TERMINATED BY '\n' ignore 1 lines.
If not, I remove the entire row. I repeat this process for all rows in the txt file.
The text file contains data of the format:
id col2 col3 2012-07-27-19:27:06 col5
id col2 col3 2012-07-25-09:58:50 col5
id col2 col3 2012-07-23-10:14:13 col5
EDIT: After reading your comments, here's the code for doing the same on tab separated data:
$handler = fopen("myfile.txt","r");
$error = false;
while (!feof($handler)){
fgets($handler,$linetocheck);
$cols = explode (chr(9), $linetocheck); //edit: using http://es.php.net/manual/en/function.fgetcsv.php you can get the same result as with fgets+explode
if (count($cols)>$max_cols){
$error=true;
break;
}
}
fclose($handler);
if (!$error){
//...do stuff
}
This code reads a file, let's say "myfile.txt", line by line, and sets variable $error to true if any of the lines has a length of more than $max_cols. (My apologies if that's not what you're asking, your question is not the most clear to me)
$handler = fopen("myfile.txt","r");
$error = false;
while (!feof($handler)){
fgets($handler,$linetocheck);
if (strlen($linetocheck)>$max_cols){
$error=true;
break;
}
}
fclose($handler);
if (!$error){
//...do stuff
}
I know it's an old thread, but I was looking something similar for myself and I came across to this topic, but none of the answers provided here helped me.
Thus, I've went ahead and came with my own solution which is tested and works perfectly (can be improved).
Assume, we have a CSV file named example.csv that contains the following dummy data (on purpose, the last line, 6th, contains one extra data then the other rows):
Name,Country,Age
John,Ireland,18
Ted,USA,22
Lisa,UK,23
Michael,USA,20
Louise,Ireland,22,11
Now, when we're checking the CSV file to assure all the rows have the same number of data, the following block of code will do the trick and pin-point on what line the error occurred:
function validateCsvColumnLength($pathToCsvFile)
{
if(!file_exists($pathToCsvFile) || !is_readable($pathToCsvFile)){
throw new \Exception('Filename doesn`t exist or is not readable.');
}
if (!$handle = fopen($pathToCsvFile, "r")) {
throw new \Exception("Stream error");
}
$rowLength = [];
$rowNumber = 0;
while (($data = fgetcsv($handle)) !== FALSE) {
$rowLength[] = count($data);
$rowNumber++;
}
fclose($handle);
$rowKeyWithError = array_search(max($rowLength), $rowLength);
$differentRowCount = count(array_unique($rowLength));
// if there's a row that has more or less data, throw an error with the line that triggered it
if ($differentRowCount !== 1) {
throw new \Exception("Error, data count from row {$rowKeyWithError} does not match header size");
}
return true;
}
To actually test it, just do a var_dump() to see the result:
var_dump(validateCsvColumnLength('example.csv'));
What columns do you mean? If you just means amount of characters in rows, just split (explode) the file into many rows and check whether their lengths are equal to 5.
If you meant columns with delimeters, then you should find amount of occurences of that splitter in each row and then again check are they equal to 5. use fgetcsv for that
I'm assuming your talking about the length of each line in the file. If so, here's a possible solution.
$file_handle = fopen("myfile", "r");
while (!feof($file_handle)) {
$line = fgets($file_handle);
if(strlen($line)!=5) {
throw new Exception("Could not save file to database.");
break;
}
}
fclose($file_handle);
Yes, it is possible. I've done that exact thing. Use PHP's csv processing functions.
You will need these functions:
fopen()
fgetcsv()
And possibly some others.
fgetcsv returns an array.
I'll give you a short example of how you can validate.
here's the csv:
col1,col2,col3,col4
1,2,3,4
1,2,3,4,
1,2,3,4,5
1,2,3,4
I'll skip the fopen part and go straight to the validation step.
Note that "\t" is the tab character.
$row_length;
$i = 0;
while($row = fgetcsv($handle,0,"\t") {
if($i == 0) {
$row_length = sizeof($row);
} else {
if(sizeof($row) != $row_length) {
echo "Error, line $i of the data does not match header size";
break;
}
}
}
That would test each row to make sure it is the same as the 1st row's ($i = 0) length.
EDIT:
And, in case you don't know how to search the internet, here is the page for fgetcsv:
http://php.net/manual/en/function.fgetcsv.php
Here is the function prototype:
array fgetcsv ( resource $handle [, int $length = 0 [, string $delimiter = ',' [, string $enclosure = '"' [, string $escape = '\' ]]]] )
As you can see, it has everything you would need for doing a quick scan in PHP before you send your data to LOAD DATA IN FILE.
I have solved your exact problem in my own program. My program also automatically eliminates duplicate rows and other cool stuff.
You can try to see if fgetcsv will suffice. If it doesn't, please be a bit more descriptive on what you mean by columns.
I want to add a string (the value of a DOM element - $entry = stripslashes($_GET["nameofmytextarea"]);) to the second line of myfile.csv (so as not to delete the header).
I don't care about CSV stuff, everything is already formatted. Just treat it as a text string being added to a text file.
I don't want anything complicated, just skip the first line and "append" above the second line: under the header but above all the other CSV lines.
How hard can that be?
$contents = explode("\n", file_get_contents('myfile.csv'), 2);
file_put_contents('myfile.csv', $contents[0]."\n".$entry."\n".$contents[1]);
This should work if the lines are separated by unix-lineendings.
If the file first looks like this:
header
content
content2
and the code is run with $entry = 'test'; it will look like this afterwards:
header
test
content
content2
A combination of file() and array_splice() is what you need here:
function prepend_to_csv ($file, $line) {
if (!$data = file($file)) {
return FALSE;
}
array_splice($data, 1, 0, $line.PHP_EOL);
return (bool) file_put_contents($file, $data);
}
if (prepend_to_csv('myfile.csv', "this,is,some,data")) {
echo 'Success';
} else {
echo 'Fail';
}
Because of the way this method works, you need to ensure that you manually add the EOL to the new line yourself.