【问题标题】:How to extract in a text file only the needed data and save it to csv using php如何仅在文本文件中提取所需的数据并使用 php 将其保存到 csv
【发布时间】:2015-08-27 07:06:20
【问题描述】:

我收到了一个文本文件,其中包含 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

顺便说一句,文本文件来自记录上述以下数据的生物识别指纹机。任何帮助,将不胜感激。提前致谢。

编辑:

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

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,也许你可以使用 bash 脚本?
  • 我是 php 新手,我尝试将其转换为数组,但无法提取所需的数据,也无法将其保存到 csv 文件中。
  • 使用您目前使用的代码编辑您的答案。尝试将文本转换为数组是一个好的开始!

标签: php csv text


【解决方案1】:

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

$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];

用这两行代码:

 $dt = explode(" ", $line[5]);
 $write[] = $line[2] . "\t" . $dt[0] . "\t" . $dt[1];

编辑 - 最终版本

好的,基于每列之间有 1 个标签的事实,除了 01408156 和 33 之间的 3 个标签,这对我有用。

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);

告诉我进展如何。

上次编辑

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

$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);

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

【讨论】:

  • 我猜我的代码没有帮助?如果您提供您正在阅读的数据的准确样本,我可以为您提供更好的帮助。您要提取哪两个字段并将其写入新文件?
  • 查看我的新帖子。我只需要第三列中的数据和最后一列中的日期时间,并将日期与时间分开,然后将其保存到 csv 文件中,但在将其保存到 csv 后我被卡住了。
  • 如何获取数据?您的原始帖子显示了一些数据,但似乎不是逗号或制表符分隔且没有标题。我看到 7 列数据,对吗?你提供的细节越多,我能提供的帮助就越大。如果可能,发布前 10 行示例数据,完全按照您获取的方式发布。
  • 好的,请查看我上面的答案以获取新的程序版本。如果它对您有用,那么请接受该帖子作为您的答案。如果您需要调整程序的日期和时间,请告诉我。
  • 好的,我会尝试您的代码,但我还需要将日期与时间分开,以便在 csv 中我可以保存三个数据,即第 2 列中的 ID、从日期时间提取的日期和时间也来自日期时间。谢谢
猜你喜欢
  • 1970-01-01
  • 2021-12-24
  • 1970-01-01
  • 1970-01-01
  • 2019-10-07
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多