如何将 .out 文件导出为 .txt 文件或 sql 文件?
How to export a .out file into a .txt file or a sql file?
如何将 .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
P.S。即使是空白的 space 也是不同段的一部分。有些段的长度为 1 个字符;在这些空白或 space 中表示 NULL 值。
例如,第一个段的长度为 1 个字符。因为,您可以看到第二条记录没有值,而是一个空白 space 来表示 NULL 值。
求推荐,谢谢
基于此处提供的答案:
我开发了这段代码,您需要使用正确的名称和每一列的长度修改 $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"
}
}
如何将 .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
P.S。即使是空白的 space 也是不同段的一部分。有些段的长度为 1 个字符;在这些空白或 space 中表示 NULL 值。
例如,第一个段的长度为 1 个字符。因为,您可以看到第二条记录没有值,而是一个空白 space 来表示 NULL 值。
求推荐,谢谢
基于此处提供的答案:
我开发了这段代码,您需要使用正确的名称和每一列的长度修改 $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"
}
}