我使用pdftotext工具从pdf中提取了这篇文章。
请查找以下文本结构
stage title1 title2 title3 title4
I value1 value2 value3
II value5 value6
stage Other1 Other2 Other3 Other4
I otherval1 otherval2 otherval3 otherval4现在,我希望使用这种方式使用适当的列和标题以CSV格式导出此文本,或者以这种方式构建数组。
[
"category" => "title1",
"score" => "value1",
],
[
"category" => "title2",
"score" => "value2",
],
[
"category" => "title3",
"score" => "value3"
],
// unable to to do this
[
"category" => "title3",
"score" => "value5"
],
[
"category" => "title4",
"score" => "value6",
],
.
.
// so on现在的问题是
我面临的问题是如何映射
这里是我的解析器代码(PHP)
$rows = explode("\n", $pdfExtractedText);
$rows = array_values(array_filter($rows));
$categories = array_values(array_filter(explode(" ", $rows[7])));
$stage1Scores = array_values(array_filter(explode(" ", $rows[8])));
$stage2Scores = array_values(array_filter(explode(" ", $rows[9])));
var_dump($categories);
var_dump($stage1Scores);
var_dump($stage2Scores);输出:
// categories
array:13 [
0 => "stage"
1 => "title1"
2 => "title2"
3 => "title3"
4 => "title4"
]
//values - Index preserved so that I can map with categories
array:14 [
0 => "I"
1 => "value1"
2 => "value2"
3 => "value3"
4 => "value4"
]
// index not preserved :(
array:2 [
0 => "II"
1 => "value5",
2 => "value6"
]发布于 2018-06-27 13:50:48
那就试试这个
$csv = "";
$csv .= implode("," , $categories) . PHP_EOL;
$csv .= implode("," , $stage1scores) . PHP_EOL;
$csv .= implode("," , $stage2scores) . PHP_EOL;然后把它写到文件里。
https://stackoverflow.com/questions/51064326
复制相似问题