0

如何将 .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     

PS 即使是空格也是不同段的一部分。一些段的长度为 1 个字符;在那些空白或空格中表示 NULL 值。

例如,第一段的长度为 1 个字符。如您所见,第二条记录没有值,而是一个空格来表示 NULL 值。

请建议,谢谢。

4

1 回答 1

0

基于此处提供的答案: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"
  }
}
于 2021-12-17T02:03:19.393 回答