从 CSV 中抓取,序列化选定的字段并修改 CSV

发布于 2024-11-15 08:19:29 字数 1324 浏览 1 评论 0原文

我有一个格式相对简单的 Excel/CSV 文件。

1|2|3|4|10|20|30
 | | | |40|50|60
 | | | |70|80|90
9|8|7|6|01|02|03
 | | | |04|05|06
 | | | |07|08|09
 | | | |10|11|12

7 个字段的完整行是单个项目,而 5、6 和 7 的重复列代表我要序列化的数据。每个主行可以有任意数量的关联部分行。

因此,对于上面的第 1 项,要序列化的数据为:

 | | | |10|20|30
 | | | |40|50|60
 | | | |70|80|90

与第 2 项相同,尽管单元格内容完全是任意的(除了项目数)。

输出将在下面获取序列化数据,除了打印到页面之外,它需要返回到 CSV,如第 8 列,并删除其他行(在本例中为 2-3、5-6)。

如果我能实现以下目标,那就太好了:

<?
$line_1 = array(
    array('1' => '10', '20', '30'), 
    array('1' => '40', '50', '60'), 
    array('1' => '70', '80', '90'), 
);
$line_2 = array(
    array('1' => '01', '02', '03'), 
    array('1' => '04', '05', '06'), 
    array('1' => '07', '08', '09'), 
    array('1' => '10', '11', '12'), 
);
echo serialize($line_1);
echo serialize($line_2);
?>  

所以最后:

1|2|3|4|10|20|30|serialize($line_1)
9|8|7|6|01|02|03|serialize($line_2)

那么这只是将其写入父行的情况。

第一行的输出如下并写入 col H:

1|2|3|4|10|20|30|a:3:{i:0;a:3:{i:1;s:2:"10";i:2;s:2:"20";i:3;s:2:"30";}i:1;a:3:{i:1;s:2:"40";i:2;s:2:"50";i:3;s:2:"60";}i:2;a:3:{i:1;s:2:"70";i:2;s:2:"80";i:3;s:2:"90";}}

是否可以在没有 PHP 干预的情况下在 Excel 中执行此操作,如果不能,如何在 PHP 中执行此操作?

I have an Excel/CSV file with relatively simple format.

1|2|3|4|10|20|30
 | | | |40|50|60
 | | | |70|80|90
9|8|7|6|01|02|03
 | | | |04|05|06
 | | | |07|08|09
 | | | |10|11|12

The complete rows of 7 fields are single items, while the repeating columns of 5, 6 and 7 represent the data I want to serialise. Each main row can have any number of partial rows associated.

So for the above, item 1, the data to serialise is:

 | | | |10|20|30
 | | | |40|50|60
 | | | |70|80|90

Same for item 2, although the cell content is completely arbitrary, except for the number of items.

Output would be below to get the serialised data, except rather than print to page it needs to go back in to the CSV, as column 8, with the additional rows (2-3, 5-6 in this case) deleted.

If I can achieve the below, then it would be great:

<?
$line_1 = array(
    array('1' => '10', '20', '30'), 
    array('1' => '40', '50', '60'), 
    array('1' => '70', '80', '90'), 
);
$line_2 = array(
    array('1' => '01', '02', '03'), 
    array('1' => '04', '05', '06'), 
    array('1' => '07', '08', '09'), 
    array('1' => '10', '11', '12'), 
);
echo serialize($line_1);
echo serialize($line_2);
?>  

So finally:

1|2|3|4|10|20|30|serialize($line_1)
9|8|7|6|01|02|03|serialize($line_2)

Then it would just be a case of getting it written to the parent line.

Output of the first line would be as follows and written to col H:

1|2|3|4|10|20|30|a:3:{i:0;a:3:{i:1;s:2:"10";i:2;s:2:"20";i:3;s:2:"30";}i:1;a:3:{i:1;s:2:"40";i:2;s:2:"50";i:3;s:2:"60";}i:2;a:3:{i:1;s:2:"70";i:2;s:2:"80";i:3;s:2:"90";}}

Is it possible to do that in Excel without PHP intervention, and if not how can it be done in PHP?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

弄潮 2024-11-22 08:19:33

在 Excel 中,将此行复制到 h1 上:

=IF(A1<>"";IF(A2="";IF(A3="";TEXT(E2;"0")&","&TEXT(F2;"0")&","&TEXT(G2;"0")&","&TEXT(E3;"0")&","&TEXT(F3;"0")&","&TEXT(G3;"0");"");""); "")

然后通过从单元格光标的右下角向下拖动,将该行复制到其下方的行上。

如果你想在 php 中执行此操作,请使用以下代码(减去文件读取):

<?php

  $array_to_serialize = array();
  $output_array = array();
  $in_lines = array();

  $in_csv = '1,2,3,4,10,20,30' . "\r\n" .
            ',,,,40,50,60' . "\r\n" .
            ',,,,70,80,90' . "\r\n" .
            '9,8,7,6,01,02,03' . "\r\n" .
            ',,,,04,05,06' . "\r\n" .
            ',,,,07,08,09';


  $in_lines = explode("\n", $in_csv);

  while (list(, $line) = each($in_lines)) {

    $line_parts = explode(",", trim($line));

    if ($line_parts[0] != '') {
      if (!empty($output_line)) {
        $output_array[] = implode(',', $output_line) . ',' . implode(',', $array_to_serialize);
      }

      $output_line = $line_parts;
      $array_to_serialize = array();
    } else {
      $array_to_serialize[] = $line_parts[4];
      $array_to_serialize[] = $line_parts[5];
      $array_to_serialize[] = $line_parts[6];
    }

  }

  $output_array[] = implode(',', $output_line) . ',' . implode(',', $array_to_serialize);

  $out_csv = implode("\r\n", $output_array);

  echo $out_csv;

?>

In excel, copy this line on h1:

=IF(A1<>"";IF(A2="";IF(A3="";TEXT(E2;"0")&","&TEXT(F2;"0")&","&TEXT(G2;"0")&","&TEXT(E3;"0")&","&TEXT(F3;"0")&","&TEXT(G3;"0");"");""); "")

then copy that line on the lines below it by dragging down from the lower right corner of the cell cursor.

if you want to do it in php, here's code for that (minus file reading):

<?php

  $array_to_serialize = array();
  $output_array = array();
  $in_lines = array();

  $in_csv = '1,2,3,4,10,20,30' . "\r\n" .
            ',,,,40,50,60' . "\r\n" .
            ',,,,70,80,90' . "\r\n" .
            '9,8,7,6,01,02,03' . "\r\n" .
            ',,,,04,05,06' . "\r\n" .
            ',,,,07,08,09';


  $in_lines = explode("\n", $in_csv);

  while (list(, $line) = each($in_lines)) {

    $line_parts = explode(",", trim($line));

    if ($line_parts[0] != '') {
      if (!empty($output_line)) {
        $output_array[] = implode(',', $output_line) . ',' . implode(',', $array_to_serialize);
      }

      $output_line = $line_parts;
      $array_to_serialize = array();
    } else {
      $array_to_serialize[] = $line_parts[4];
      $array_to_serialize[] = $line_parts[5];
      $array_to_serialize[] = $line_parts[6];
    }

  }

  $output_array[] = implode(',', $output_line) . ',' . implode(',', $array_to_serialize);

  $out_csv = implode("\r\n", $output_array);

  echo $out_csv;

?>
眉目亦如画i 2024-11-22 08:19:32

因为问题有点改变,所以我要给出新的答案。希望这有帮助。

<?php

  $array_to_serialize = array();
  $output_array = array();
  $in_lines = array();

  $in_csv = '1,2,3,4,10,20,30' . "\r\n" .
            ',,,,40,50,60' . "\r\n" .
            ',,,,70,80,90' . "\r\n" .
            '9,8,7,0,01,02,03' . "\r\n" .
            ',,,,40,11,60' . "\r\n" .
            ',,,,70,80,90' . "\r\n" .
            '9,8,7,6,01,02,03' . "\r\n" .
            ',,,,04,05,06' . "\r\n" .
            ',,,,07,08,09';


  $in_lines = explode("\n", $in_csv);

  while (list(, $line) = each($in_lines)) {

    $line_parts = explode(",", trim($line));

    if ($line_parts[0] != '') {

      if (!empty($beginning_numbers)) {
        $output_array[] = implode(',', $beginning_numbers) . ',' . serialize($matrix_part);
      }

      $beginning_numbers = array_slice($line_parts, 0, 4);
      $matrix_part = array(array_slice($line_parts, 4));

      $output_line = $line_parts;
      $array_to_serialize = array();
    } else {
      $matrix_part[] = array_slice($line_parts, 4);
    }

  }

  $output_array[] = implode(',', $beginning_numbers) . ',' . serialize($matrix_part);

  $out_text = implode("\r\n", $output_array);

  echo $out_text;

?>

Because the question changed a bit, I'm going to give a new answer. Hope this helps.

<?php

  $array_to_serialize = array();
  $output_array = array();
  $in_lines = array();

  $in_csv = '1,2,3,4,10,20,30' . "\r\n" .
            ',,,,40,50,60' . "\r\n" .
            ',,,,70,80,90' . "\r\n" .
            '9,8,7,0,01,02,03' . "\r\n" .
            ',,,,40,11,60' . "\r\n" .
            ',,,,70,80,90' . "\r\n" .
            '9,8,7,6,01,02,03' . "\r\n" .
            ',,,,04,05,06' . "\r\n" .
            ',,,,07,08,09';


  $in_lines = explode("\n", $in_csv);

  while (list(, $line) = each($in_lines)) {

    $line_parts = explode(",", trim($line));

    if ($line_parts[0] != '') {

      if (!empty($beginning_numbers)) {
        $output_array[] = implode(',', $beginning_numbers) . ',' . serialize($matrix_part);
      }

      $beginning_numbers = array_slice($line_parts, 0, 4);
      $matrix_part = array(array_slice($line_parts, 4));

      $output_line = $line_parts;
      $array_to_serialize = array();
    } else {
      $matrix_part[] = array_slice($line_parts, 4);
    }

  }

  $output_array[] = implode(',', $beginning_numbers) . ',' . serialize($matrix_part);

  $out_text = implode("\r\n", $output_array);

  echo $out_text;

?>
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文