-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathcsv_normalize.php
More file actions
47 lines (40 loc) · 1.21 KB
/
csv_normalize.php
File metadata and controls
47 lines (40 loc) · 1.21 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
<?php
/**
* Converte todos os arquivos da pasta data em versão normalizada.
* Se não houverem alteração, não sobrescreve.
* php src/csv_normalize.php
*/
$here = dirname(__FILE__);
fwrite(STDERR, "\n-------------\n BEGIN of check CSV files");
foreach (glob("$here/../data/*.csv") as $f) {
fwrite(STDERR, "\n $f " . round(filesize($f)/1024,1) . "kb" );
csv_normalize($f);
}
fwrite(STDERR, "\n END\n");
///////////////////
/**
* Standard "get array from CSV", file or CSV-string.
* CSV conventions by default options of the build-in str_getcsv() function.
* @param $f string file (.csv) with path or CSV string (with more tham 1 line).
* @return array of arrays.
*/
function getCsv($f) {
return array_map( 'str_getcsv', file($f) );
}
/**
* Check CSV file and overwrite by a normalized one when need.
*/
function csv_normalize($f) {
$f2 = "/tmp/testCsv_".rand();
$h = fopen($f2, 'w');
foreach( getCsv($f) as $r)
fputcsv($h,$r);
fclose($h);
if (file_get_contents($f)==file_get_contents($f2)) {
fwrite(STDERR, "\n\tWas good, CSV normalized." );
unlink($f2);
} else {
fwrite(STDERR, "\n\tOverwriting CSV! by a normalized one.");
if (!rename($f2, $f)) die("\n Failed to rename $f2 to $f\n");
}
}