从 CSV 中抓取,序列化选定的字段并修改 CSV
我有一个格式相对简单的 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
在 Excel 中,将此行复制到 h1 上:
然后通过从单元格光标的右下角向下拖动,将该行复制到其下方的行上。
如果你想在 php 中执行此操作,请使用以下代码(减去文件读取):
In excel, copy this line on h1:
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):
因为问题有点改变,所以我要给出新的答案。希望这有帮助。
Because the question changed a bit, I'm going to give a new answer. Hope this helps.