我編制了這個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'? –
@AnnaJeanine是否在cli命令中添加了文件名?就像'php script.php fileWithTabs' –
是的!我使用了'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「}'..所以它沒有跳過那一行..我怎麼能確保第一個製表符分隔線被跳過? –