dongyuli0964
2017-02-21 17:47
浏览 83

迭代CSV文件并获取指定标题的每个值?

I have a CSV file and I want to check if the row contains a special title. Only if my row contains a special title it should be converted to XML, other stuff added and so on.

My question now is, how can I iterate through the whole CSV file and get for every title the value in this field?

Because if it matches my special title I just want to convert the specified row where the title is matching my title. Maybe also an idea how I can do that?

Sample: CSV File

I must add that feature to my actual function. Because my actual function is just is converting the whole CSV to XML. But I just want to convert the specified rows.

My actual function:

function csvToXML($inputFilename, $outputFilename, $delimiter = ',')
{
  // Open csv to read
  $inputFile = fopen($inputFilename, 'rt');

  // Get the headers of the file
  $headers = fgetcsv($inputFile, 0, $delimiter);

  // Create a new dom document with pretty formatting
  $doc = new DOMDocument('1.0', 'utf-8');
  $doc->preserveWhiteSpace = false;
  $doc->formatOutput = true;

  // Add a root node to the document
  $root = $doc->createElement('products');
  $root = $doc->appendChild($root);

  // Loop through each row creating a <row> node with the correct data
  while (($row = fgetcsv($inputFile, 0, $delimiter)) !== false) {
    $container = $doc->createElement('product');
    foreach ($headers as $i => $header) {
      $child = $doc->createElement($header);
      $child = $container->appendChild($child);
      $value = $doc->createTextNode($row[$i]);
      $value = $child->appendChild($value);
    }

    $root->appendChild($container);
  }

  $strxml = $doc->saveXML();
  $handle = fopen($outputFilename, 'w');
  fwrite($handle, $strxml);
  fclose($handle);
}

图片转代码服务由CSDN问答提供 功能建议

我有一个CSV文件,我想检查该行是否包含特殊标题。 只有当我的行包含特殊标题时,才应将其转换为XML,添加其他内容等等。

我现在的问题是,如何遍历整个CSV文件并获取 每个标题都是此字段中的值?

因为如果它与我的特殊标题匹配,我只想转换标题与我的标题匹配的指定行。 也许我也知道如何做到这一点?

示例: CSV文件

我必须将该功能添加到我的实际功能中。 因为我的实际功能只是将整个CSV转换为XML。 但我只想转换指定的行。

我的实际功能:

  function csvToXML($ inputFilename,$ outputFilename,$ delimiter =',')
 {
  //打开csv读取
 $ inputFile = fopen($ inputFilename,'rt'); 
 
 //获取文件的头文件
 $ headers = fgetcsv($ inputFile,0,$ delimiter); \  n 
 //创建一个具有漂亮格式的新dom文档
 $ doc = new DOMDocument('1.0','utf-8'); 
 $ doc-&gt; preserveWhiteSpace = false; 
 $ doc-&gt;  formatOutput = true; 
 
 //为文档添加根节点
 $ root = $ doc-&gt; createElement('products'); 
 $ root = $ doc-&gt; appendChild($ root);  
 
 //遍历每一行,创建一个&lt; row&gt; 具有正确数据的节点
 while(($ row = fgetcsv($ inputFile,0,$ delimiter))!== false){
 $ container = $ doc-&gt; createElement('product'); 
 foreach  ($ header为$ i =&gt; $ header){
 $ child = $ doc-&gt; createElement($ header); 
 $ child = $ container-&gt; appendChild($ child); 
 $ value =  $ doc-&gt; createTextNode($ row [$ i]); 
 $ value = $ child-&gt; appendChild($ value); 
} 
 
 $ root-&gt; appendChild($ container); \  n} 
 
 $ strxml = $ doc-&gt; saveXML(); 
 $ handle = fopen($ outputFilename,'w'); 
 fwrite($ handle,$ strxml); 
 fclose($ handle  ); 
} 
   
 
  • 写回答
  • 好问题 提建议
  • 关注问题
  • 收藏
  • 邀请回答

1条回答 默认 最新

  • dongliyu3278 2017-02-21 18:03
    已采纳

    Just check the title before adding the rows to XML. You could do it by adding the following lines:

     while (($row = fgetcsv($inputFile, 0, $delimiter)) !== false) {
    
        $specialTitles = Array('Title 1', 'Title 2', 'Title 3'); // titles you want to keep
    
        if(in_array($row[1], $specialTitles)){
            $container = $doc->createElement('product');
            foreach ($headers as $i => $header) {
              $child = $doc->createElement($header);
              $child = $container->appendChild($child);
              $value = $doc->createTextNode($row[$i]);
              $value = $child->appendChild($value);
            }
    
            $root->appendChild($container);
        }
      }
    
    已采纳该答案
    评论
    解决 无用
    打赏 举报

相关推荐 更多相似问题