2016-12-01 75 views
0

我編制了這個Python腳本來讀取製表符分隔的文件,並將'\t'中的行以array開頭。我用這個代碼:閱讀並解析製表符分隔的文件PHP

import sys 
from collections import OrderedDict 
import json 
import os 

file = sys.argv[1] 

f = open(file, 'r') 
direc = '/dir/to/JSONs/' 
fileJSON = sys.argv[1]+'.json' 

key1 = OrderedDict() 
summary_data = [] 
full_path = os.path.join(direc,fileJSON) 

Read = True 
for line in f: 
     if line.startswith("#"): 
      Read = True 

     elif line.startswith('\tC'): 
      Read= True 

     elif line.startswith('\t') and Read == True: 
      summary = line.strip().split('\t') 
      key1[summary[1]]=int(summary[0]) 
      Read = True  

summary_data.append(key1) 
data = json.dumps(summary_data) 
with open(full_path, 'w') as datafile: 
    datafile.write(data) 
print(data) 

這我解析的數據:

# BUSCO was run in mode: genome 

    C:98.0%[S:97.0%,D:1.0%],F:0.5%,M:1.5%,n:1440 

    1411 Complete BUSCOs (C) 
    1397 Complete and single-copy BUSCOs (S) 
    14 Complete and duplicated BUSCOs (D) 
    7 Fragmented BUSCOs (F) 
    22 Missing BUSCOs (M) 
    1440 Total BUSCO groups searched 

但是,我需要在PHP這個代碼。我已經設法在PHP和打開文件讀這個!有人可以幫我嗎?

回答

1

我沒有讀變量的點 - 它總是在你的代碼真,最後的'elif'聲明就足夠了。以下是您的腳本的PHP版本

<?php 
    $fileName = $argv[1]; 
    $dir = '/dir/to/JSONs/'; 
    $fullPath = $dir . $fileName . '.json'; 

    $data = []; 
    $output = fopen($fileName, 'r'); 
    while (($line = fgets($output)) !== false) { 
     if ($line[0] == "\t") { 
      $summary = explode("\t", trim($line)); 
      if (count($summary) > 1) { 
       $data[$summary[1]] = (int)$summary[0]; 
      } 
     } 
    } 

    $strData = json_encode([$data]); 
    $input = fopen($fullPath, 'w+'); 
    fwrite($input, $strData); 
    echo $strData; 
+0

謝謝!這返回'未定義的抵消:1'? –

+0

@AnnaJeanine是否在cli命令中添加了文件名?就像'php script.php fileWithTabs' –

+0

是的!我使用了'var_dump($ summary)',它返回:'array(1){[0] => string(44)「C:98.0%[S:97.0%,D:1.0%],F:0.5% M:1.5%,n:1440「}'..所以它沒有跳過那一行..我怎麼能確保第一個製表符分隔線被跳過? –

0

讀變量是在你的代碼不必要的,所以我刪除了,取而代之的東西,你可以看到在控制檯結果:

<?php 
$file = $argv[1]; 
$direc = '/dir/to/JSONs/'; 
$key1 = []; 
$summary_data = []; 
$full_path = $direc.$file.'.json'; 
$file_handler = fopen($full_path, 'r'); 
if($file_handler){ 
    while(($line = fgets($file_handler)) !== false){ 
     if($line[0] == "#" || substr($line, 0 , 2) == "\tC" || empty($line) == true){ 
      echo 'line found : '.$line; 
      continue; 
     }else{ 
      $summary = explode("\t", $line); 
      echo 'summary : '.print_r($summary,true); 
      $key1[str_replace(["\r","\n"], '', $summary[2])] = (int) $summary[1]; 
     } 
    } 
}else{ 
    echo 'Couldn\'t open file.'; 
    exit(); 
} 
array_push($summary_data, $key1); 
$data = json_encode($summary_data); 
fclose($file_handler); 
file_put_contents($full_path, $data); 
+0

謝謝!我將'$ summary [2]'更改爲'$ summary [1]'和'$ summary [1]'爲'$ summary [0]',但它返回'Undefined offset:1' –

+0

我解決了這個問題未定義的抵消問題使用它,因爲我寫道。您應該使用$ summary [2]和$ summary [1] –