如何将.out文件转换为.txt文件、CSV或SQL文件?.out文件中的记录没有分隔符,而是记录中的每个段都有一个固定的起点和终点。有没有一个在线工具或一个简单的PHP脚本,我可以在其中轻松定义每个片段的起点和终点?然后,最后,在记录的每个段之间使用分隔符|导出它。不一定,如果我可以直接将这个.out文件导出到sql文件中。
记录示例:
I299207075410 07 OCCLUSAL-HP LIQ17% LMedicis B000001000000000001EA 8428010080529100 1072363 20030101000000016750000000016750000000000167500200101010000000000000000000000000000000001218000000000000000000000000000000000000000000000000020021231262436018510(W/BRUSH APPLICATOR) TPLIQ
299207085060R01 LUZU CRE1% SBausch C000006000000000001EA 8404080054930829 1 1309011 20180105000000590530000000098421700000000902967000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000 TPCRE
附言:即使是空格也是不同部分的一部分。有些段的长度为1个字符;在这些空白或空格中表示NULL值。
例如,第一段的长度为1个字符。正如,您可以看到第二条记录没有值,而是用空格表示NULL值。
请提出建议,谢谢。
基于此处提供的答案:https://stackoverflow.com/a/62157247/1427345
我开发了这段代码,您需要使用每列的正确名称和长度来修改$fields
变量。我只是猜测而已。
<?php
$raw = file('data.out');
$fields = [
'id' => 1,
'id2' => 12,
'code' => 5,
'category' => 35,
'code2' => 32,
'category2' => 22,
'code3' => 22,
'code5' => 17,
'code6' => 2,
'code7' => 10,
'code8' => 186,
'code9' => 10
];
$unpack = [];
foreach ($fields as $name => $length) {
$unpack[] = 'A'.$length.$name;
}
$unpack_string = implode('/', $unpack);
$data = [];
foreach ($raw as $line) {
$data[] = unpack($unpack_string, $line);
}
var_dump($data);
$export = fopen("data.csv", "w");
foreach ($data as $row) {
fputcsv($export, $row, "|");
}
fclose($export);
结果:
array(2) {
[0]=>
array(12) {
["id"]=>
string(1) "I"
["id2"]=>
string(12) "299207075410"
["code"]=>
string(3) " 07"
["category"]=>
string(11) "OCCLUSAL-HP"
["code2"]=>
string(6) "LIQ17%"
["category2"]=>
string(8) "LMedicis"
["code3"]=>
string(21) "B000001000000000001EA"
["code5"]=>
string(16) "8428010080529100"
["code6"]=>
string(0) ""
["code7"]=>
string(7) "1072363"
["code8"]=>
string(181) "20030101000000016750000000016750000000000167500200101010000000000000000000000000000000001218000000000000000000000000000000000000000000000000020021231262436018510(W/BRUSH APPLICATOR)"
["code9"]=>
string(5) "TPLIQ"
}
[1]=>
array(12) {
["id"]=>
string(0) ""
["id2"]=>
string(12) "299207085060"
["code"]=>
string(3) "R01"
["category"]=>
string(4) "LUZU"
["code2"]=>
string(5) "CRE1%"
["category2"]=>
string(7) "SBausch"
["code3"]=>
string(21) "C000006000000000001EA"
["code5"]=>
string(16) "8404080054930829"
["code6"]=>
string(1) "1"
["code7"]=>
string(7) "1309011"
["code8"]=>
string(149) "20180105000000590530000000098421700000000902967000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000"
["code9"]=>
string(5) "TPCRE"
}
}