如何在文本文件中只提取所需的数据,并使用php将其保存到csv

如何在文本文件中只提取所需的数据,并使用php将其保存到csv,php,csv,text,Php,Csv,Text,我得到了一个文本文件,其中包含100多行6列,但我需要的数据位于第3列和第6列,并将其保存到csv文件中。文本文件包含如下内容: 00001 1 01408156 33 0 2014/11/17 15:21:18 00002 1 00000007 33 0 2014/11/17 15:39:59 00003 1 01409179 33 0 2014/11/18

我得到了一个文本文件,其中包含100多行6列,但我需要的数据位于第3列和第6列,并将其保存到csv文件中。文本文件包含如下内容:

00001   1   01408156                33  0   2014/11/17  15:21:18
00002   1   00000007                33  0   2014/11/17  15:39:59
00003   1   01409179                33  0   2014/11/18  07:39:45
00004   1   01410352                33  0   2014/11/18  07:40:07
00005   1   01404048                33  0   2014/11/18  07:40:12
00006   1   01411402                33  0   2014/11/18  07:40:30
00007   1   01409227                33  0   2014/11/18  07:40:34
00008   1   01410323                33  0   2014/11/18  07:40:43
00009   1   01409242                33  0   2014/11/18  07:40:46
00010   1   01010042                33  0   2014/11/18  07:40:49
00011   1   01409192                33  0   2014/11/18  07:40:53
00012   1   01409192                33  0   2014/11/18  07:40:56
00013   1   01409171                33  0   2014/11/18  07:41:21
00014   1   01403005                33  0   2014/11/18  07:41:34
00015   1   01010205                33  0   2014/11/18  07:42:00
00016   1   01411465                33  0   2014/11/18  07:42:07
00017   1   01411381                33  0   2014/11/18  07:42:13
00018   1   01403018                33  0   2014/11/18  07:42:20
00019   1   01411447                33  0   2014/11/18  07:42:24
00020   1   01410308                33  0   2014/11/18  07:42:31
00021   1   01411381                33  0   2014/11/18  07:42:36
00022   1   01411427                33  0   2014/11/18  07:43:15
00023   1   01404029                33  0   2014/11/18  07:43:28
00024   1   01411452                33  0   2014/11/18  07:43:58
00025   1   01404061                33  0   2014/11/18  07:44:07
00026   1   01409278                33  0   2014/11/18  07:44:11
00027   1   01409266                33  0   2014/11/18  07:44:17
00028   1   01404113                33  0   2014/11/18  07:44:21
$file = fopen('sample_file.csv', 'r');
$fields = array();
if($file)
{
 while(($data=fgetcsv($file, ',')) !== false)
 {
  if(empty($fields))
  {
   $fields = $data;
   continue;
  }
  $output[] = array_combine($fields, $data);
 }
 fclose($file);

// After the above code runs you will have an array named $output that contains all the data from the source file.
// Then you just use a loop to read the array and extract the columns you want and write them to a file.
// The loop would look something like this:

 foreach($output as $main)
 {
  echo("$main['Heading1'], $main['Heading2'], $main['Heading3']");
 }
}
顺便说一句,文本文件来自一台生物识别指纹机,它记录了上面的以下数据。任何帮助都将不胜感激。提前谢谢

编辑:

我尝试了这段代码,但当我试图提取必要的数据时,我得到了一个偏移错误:

if (file_exists($myFile))
{
 $fileContent = file($myFile);

 foreach($fileContent as $line_num => $line) {
 { 
   $data = explode(", ", $line);

   $fileUsername[] = trim($data[0]);
   $filePassword[] = trim($data[1]);
 }
}
这是我成功提取所需数据的代码,但我希望分解最后一列以分离日期和时间:

$file=fopen("D:/Documents/My Documents/GLG_001_12215C.txt","r");

$i=0;

$line=array();
while(($data=fgetcsv($file,1000,"\t"))!==FALSE){
if($i>0){
$data[0]="";
$data[1]="";
$data[3]="";
$data[4]="";
$data[5]="";

unset($data[0],$data[1],$data[3],$data[4],$data[5]);
$line[]=$data;
} 
$i++;
//print_r(array_values($data));

}

fclose($file);
//array_shift($line);

$converted=fopen("C:/xampp/htdocs/HRMS/temprec.txt","w");

foreach($line as $li){
fputcsv($converted,$li);
//print_r(array_values($li));
}
fclose($converted);

echo "import successful";

源数据是否包含标题?根据您的示例帖子,文件是以逗号分隔的,对吗?如果是,那么我将使用PHP读取示例文件并创建一个数组。大概是这样的:

00001   1   01408156                33  0   2014/11/17  15:21:18
00002   1   00000007                33  0   2014/11/17  15:39:59
00003   1   01409179                33  0   2014/11/18  07:39:45
00004   1   01410352                33  0   2014/11/18  07:40:07
00005   1   01404048                33  0   2014/11/18  07:40:12
00006   1   01411402                33  0   2014/11/18  07:40:30
00007   1   01409227                33  0   2014/11/18  07:40:34
00008   1   01410323                33  0   2014/11/18  07:40:43
00009   1   01409242                33  0   2014/11/18  07:40:46
00010   1   01010042                33  0   2014/11/18  07:40:49
00011   1   01409192                33  0   2014/11/18  07:40:53
00012   1   01409192                33  0   2014/11/18  07:40:56
00013   1   01409171                33  0   2014/11/18  07:41:21
00014   1   01403005                33  0   2014/11/18  07:41:34
00015   1   01010205                33  0   2014/11/18  07:42:00
00016   1   01411465                33  0   2014/11/18  07:42:07
00017   1   01411381                33  0   2014/11/18  07:42:13
00018   1   01403018                33  0   2014/11/18  07:42:20
00019   1   01411447                33  0   2014/11/18  07:42:24
00020   1   01410308                33  0   2014/11/18  07:42:31
00021   1   01411381                33  0   2014/11/18  07:42:36
00022   1   01411427                33  0   2014/11/18  07:43:15
00023   1   01404029                33  0   2014/11/18  07:43:28
00024   1   01411452                33  0   2014/11/18  07:43:58
00025   1   01404061                33  0   2014/11/18  07:44:07
00026   1   01409278                33  0   2014/11/18  07:44:11
00027   1   01409266                33  0   2014/11/18  07:44:17
00028   1   01404113                33  0   2014/11/18  07:44:21
$file = fopen('sample_file.csv', 'r');
$fields = array();
if($file)
{
 while(($data=fgetcsv($file, ',')) !== false)
 {
  if(empty($fields))
  {
   $fields = $data;
   continue;
  }
  $output[] = array_combine($fields, $data);
 }
 fclose($file);

// After the above code runs you will have an array named $output that contains all the data from the source file.
// Then you just use a loop to read the array and extract the columns you want and write them to a file.
// The loop would look something like this:

 foreach($output as $main)
 {
  echo("$main['Heading1'], $main['Heading2'], $main['Heading3']");
 }
}
您需要编写代码将数据写入文件,而不是echo命令

编辑-新程序:

这里有一个新的程序可以尝试。既然你同意我的观点,有7个专栏,这个版本就行了。然而,PHP日期和时间通常在日期和时间之间有一个空格,而不是一个标签。如果有一个空间,而不是一个标签,那么这个版本将不得不调整一点

if(!$myfile = fopen("data.txt", "r"))
{
 echo("Unable to open data file!");
 exit;
}
while(!feof($myfile))
{
 $line = explode("\t", fgets($myfile));
 $write[] = $line[2] . "\t" . $line[5] . "\t" . $line[6];
}
fclose($myfile);
if(!$handle = fopen("temp.csv", "w"))
{
 echo("Can't open file for writing!");
 exit;
}
foreach($write as $line)
{
 if(fwrite($handle, $line) === FALSE)
 {
  echo("Can't write to file!");
  exit;
 }
}
fclose($handle);
享受吧

第二次编辑:

好的,只需替换这行代码:

 $write[] = $line[2] . "\t" . $line[5] . "\t" . $line[6];
$line = feof($myfile);
使用以下两行代码:

 $dt = explode(" ", $line[5]);
 $write[] = $line[2] . "\t" . $dt[0] . "\t" . $dt[1];
编辑-最终版本

好的,基于每列之间有一个标签,除了0148156和33之间有三个标签,这对我来说很有用

if(!$myfile = fopen("data.txt", "r"))
{
 echo("Unable to open data file!");
 exit;
}
// The below line is to read the headers and discard
$line = feof($myfile);
while(!feof($myfile))
{
 $line = explode("\t", fgets($myfile));
 $dt = explode(" ", $line[7]);
 $write[] = $line[2] . "\t" . $dt[0] . "\t" . $dt[1];
}
fclose($myfile);
if(!$handle = fopen("temp.csv", "w"))
{
 echo("Can't open file for writing!");
 exit;
}
foreach($write as $line)
{
 if(fwrite($handle, $line) === FALSE)
 {
  echo("Can't write to file!");
  exit;
 }
}
fclose($handle);
让我知道进展如何

最后编辑

我在示例中添加了标题,发现我犯了一个错误。这行代码:

 $write[] = $line[2] . "\t" . $line[5] . "\t" . $line[6];
$line = feof($myfile);
需要更改为:

$line = fgets($myfile);
除非您希望在输出文件中包含3个标题

新的最终版本

这是一个不使用数组构建输出数据的版本。此版本将同时读取输入和写入输出

if(!$input = fopen("data.txt", "r"))
{
 echo("Unable to open data file!");
 exit;
}
if(!$output = fopen("temp.csv", "w"))
{
 echo("Can't open file for writing!");
 exit;
}
// The below line is to read the headers and discard
$line = fgets($input);
while(!feof($input))
{
 $line = explode("\t", fgets($input));
 $dt = explode(" ", $line[7]);
 if(fwrite($output, $line[2] . "\t" . $dt[0] . "\t" . $dt[1]) === FALSE)
 {
  echo("Can't write to file!");
  exit;
 }
}
fclose($input);
fclose($output);

让我知道它是如何工作的。

到目前为止你做了什么?请发布一些代码并向我们展示您的尝试,以帮助我们帮助您。你不应该需要PHP,也许你可以使用bash脚本?我对PHP不熟悉,我曾尝试将其转换为数组,但我无法提取所需的数据,也无法将其保存到csv文件。请使用迄今为止使用的代码编辑你的答案。尝试将文本转换为数组是一个好的开始!我想我的代码没用吧?如果你能提供你正在阅读的数据的准确样本,我会帮你更好。你想提取哪两个字段并写入新文件?查看我的新帖子。我只需要第三列中的数据和最后一列中的日期时间,并在将其保存到csv文件之前将日期和时间分开,但在将其保存到csv文件后,我被卡住了。如何获取数据?您的原始帖子显示了一些数据,但似乎不是逗号或制表符分隔的,也没有标题。我看到7列数据,对吗?你提供的细节越多,我就越能帮上忙。如果可能的话,发布前10行示例数据,完全按照您获得它的方式。好的,请查看我上面的答案以获得新的程序版本。如果它对你有效,那么请接受这个帖子作为你的答案。如果您需要调整程序的日期和时间,请告诉我。好的,我将尝试您的代码,但我还需要将日期与时间分开,以便在csv中我可以保存三个数据,即第2列中的ID、从datetime提取的日期和也从datetime提取的时间。非常感谢。