如何获取外部可执行文件的二维数组输出?
import os
import subprocess
proc = subprocess.Popen(['C:\\bin\\test.exe', '-b10.10.2000','-house50.20E,10.40N','-hsyE','-utc00.18','-eswe','-sid27','-fPls','-head','-g'], stdout=subprocess.PIPE).communicate()
输出-
>>> print (proc)
(b'Sun \t 173.1483122\t 0.9880656\r\nMoon \t 313.0306463\t 12.386413
2\r\nMercury \t 198.1580011\t 0.7897290\r\nVenus \t 204.8965900\t 1.2175
754\r\nMars \t 140.5897149\t 0.6242807\r\nJupiter \t 47.2090321\t -0.035
0736\r\nSaturn \t 36.4696238\t -0.0472129\r\nUranus \t 293.1678525\t -0.01
36009\r\nNeptune \t 279.9571737\t -0.0031065\r\nPluto \t 226.9850014\t 0.
0259009\r\nmean Node \t 86.2385757\t -0.0529828\r\ntrue Node \t 86.8486961\t -0.
1297016\r\nmean Apogee \t 270.9593053\t 0.1107212\r\nhouse 1 \t 135.1767780\r\n
house 2 \t 165.1767780\r\nhouse 3 \t 195.1767780\r\nhouse 4 \t 225.17
67780\r\nhouse 5 \t 255.1767780\r\nhouse 6 \t 285.1767780\r\nhouse 7
\t 315.1767780\r\nhouse 8 \t 345.1767780\r\nhouse 9 \t 15.1767780\r\nhouse 1
0 \t 45.1767780\r\nhouse 11 \t 75.1767780\r\nhouse 12 \t 105.1767780\r\n
Ascendant \t 135.1767780\r\nMC \t 46.8522202\r\nARMC \t 69.1022
636\r\nVertex \t 351.3888546\r\nequat. Asc. \t 133.5633351\r\nco-Asc. W.Koch \t
131.6974530\r\nco-Asc Munkasey\t 149.4317411\r\nPolar Asc. \t 311.6974530\r\n', None)
>>>
该数组应类似于此原始输出-
Sun 173.1483122 0.9880656
Moon 313.0306463 12.3864132
Mercury 198.1580011 0.7897290
Venus 204.8965900 1.2175754
Mars 140.5897149 0.6242807
Jupiter 47.2090321 -0.0350736
Saturn 36.4696238 -0.0472129
Uranus 293.1678525 -0.0136009
Neptune 279.9571737 -0.0031065
Pluto 226.9850014 0.0259009
mean Node 86.2385757 -0.0529828
true Node 86.8486961 -0.1297016
mean Apogee 270.9593053 0.1107212
house 1 135.1767780
house 2 165.1767780
house 3 195.1767780
house 4 225.1767780
house 5 255.1767780
house 6 285.1767780
house 7 315.1767780
house 8 345.1767780
house 9 15.1767780
house 10 45.1767780
house 11 75.1767780
house 12 105.1767780
Ascendant 135.1767780
MC 46.8522202
ARMC 69.1022636
Vertex 351.3888546
equat. Asc. 133.5633351
co-Asc. W.Koch 131.6974530
co-Asc Munkasey 149.4317411
Polar Asc. 311.6974530
0
答案 0 :(得分:2)
它看起来像CSV文件(逗号分隔值),但带有制表符而不是逗号。
因此您可以将数据保存在文件中,并使用模块csv
进行读取。
或者您可以使用io.StringIO
在内存中创建假文件并使用csv
进行读取
data = (b'Sun \t 173.1483122\t 0.9880656\r\nMoon \t 313.0306463\t 12.386413\
2\r\nMercury \t 198.1580011\t 0.7897290\r\nVenus \t 204.8965900\t 1.2175\
754\r\nMars \t 140.5897149\t 0.6242807\r\nJupiter \t 47.2090321\t -0.035\
0736\r\nSaturn \t 36.4696238\t -0.0472129\r\nUranus \t 293.1678525\t -0.01\
36009\r\nNeptune \t 279.9571737\t -0.0031065\r\nPluto \t 226.9850014\t 0.\
0259009\r\nmean Node \t 86.2385757\t -0.0529828\r\ntrue Node \t 86.8486961\t -0.\
1297016\r\nmean Apogee \t 270.9593053\t 0.1107212\r\nhouse 1 \t 135.1767780\r\n\
house 2 \t 165.1767780\r\nhouse 3 \t 195.1767780\r\nhouse 4 \t 225.17\
67780\r\nhouse 5 \t 255.1767780\r\nhouse 6 \t 285.1767780\r\nhouse 7\
\t 315.1767780\r\nhouse 8 \t 345.1767780\r\nhouse 9 \t 15.1767780\r\nhouse 1\
0 \t 45.1767780\r\nhouse 11 \t 75.1767780\r\nhouse 12 \t 105.1767780\r\n\
Ascendant \t 135.1767780\r\nMC \t 46.8522202\r\nARMC \t 69.1022\
636\r\nVertex \t 351.3888546\r\nequat. Asc. \t 133.5633351\r\nco-Asc. W.Koch \t\
131.6974530\r\nco-Asc Munkasey\t 149.4317411\r\nPolar Asc. \t 311.6974530\r\n', None)
import io
import csv
# output from process
text = data[0].decode()
print(text)
# create fake file in memory with text data
fake_file = io.StringIO(text)
# create CSV reader
reader = csv.reader(fake_file, delimiter='\t')
# read all data from CSV file
data = list(reader)
# remove spaces
data = [[x.strip() for x in row] for row in data]
for row in data:
print(row)
结果-行
['Sun', '173.1483122', '0.9880656']
['Moon', '313.0306463', '12.3864132']
['Mercury', '198.1580011', '0.7897290']
['Venus', '204.8965900', '1.2175754']
['Mars', '140.5897149', '0.6242807']
['Jupiter', '47.2090321', '-0.0350736']
['Saturn', '36.4696238', '-0.0472129']
['Uranus', '293.1678525', '-0.0136009']
['Neptune', '279.9571737', '-0.0031065']
['Pluto', '226.9850014', '0.0259009']
['mean Node', '86.2385757', '-0.0529828']
['true Node', '86.8486961', '-0.1297016']
['mean Apogee', '270.9593053', '0.1107212']
['house 1', '135.1767780']
['house 2', '165.1767780']
['house 3', '195.1767780']
['house 4', '225.1767780']
['house 5', '255.1767780']
['house 6', '285.1767780']
['house 7', '315.1767780']
['house 8', '345.1767780']
['house 9', '15.1767780']
['house 10', '45.1767780']
['house 11', '75.1767780']
['house 12', '105.1767780']
['Ascendant', '135.1767780']
['MC', '46.8522202']
['ARMC', '69.1022636']
['Vertex', '351.3888546']
['equat. Asc.', '133.5633351']
['co-Asc. W.Koch', '131.6974530']
['co-Asc Munkasey', '149.4317411']
['Polar Asc.', '311.6974530']
第二种方法:
您来自流程的数据是一个字符串,因此您也可以使用split('\n')
,split('\t')
,strip()
创建二维列表。
data = (b'Sun \t 173.1483122\t 0.9880656\r\nMoon \t 313.0306463\t 12.386413\
2\r\nMercury \t 198.1580011\t 0.7897290\r\nVenus \t 204.8965900\t 1.2175\
754\r\nMars \t 140.5897149\t 0.6242807\r\nJupiter \t 47.2090321\t -0.035\
0736\r\nSaturn \t 36.4696238\t -0.0472129\r\nUranus \t 293.1678525\t -0.01\
36009\r\nNeptune \t 279.9571737\t -0.0031065\r\nPluto \t 226.9850014\t 0.\
0259009\r\nmean Node \t 86.2385757\t -0.0529828\r\ntrue Node \t 86.8486961\t -0.\
1297016\r\nmean Apogee \t 270.9593053\t 0.1107212\r\nhouse 1 \t 135.1767780\r\n\
house 2 \t 165.1767780\r\nhouse 3 \t 195.1767780\r\nhouse 4 \t 225.17\
67780\r\nhouse 5 \t 255.1767780\r\nhouse 6 \t 285.1767780\r\nhouse 7\
\t 315.1767780\r\nhouse 8 \t 345.1767780\r\nhouse 9 \t 15.1767780\r\nhouse 1\
0 \t 45.1767780\r\nhouse 11 \t 75.1767780\r\nhouse 12 \t 105.1767780\r\n\
Ascendant \t 135.1767780\r\nMC \t 46.8522202\r\nARMC \t 69.1022\
636\r\nVertex \t 351.3888546\r\nequat. Asc. \t 133.5633351\r\nco-Asc. W.Koch \t\
131.6974530\r\nco-Asc Munkasey\t 149.4317411\r\nPolar Asc. \t 311.6974530\r\n', None)
text = data[0].decode()
print(text)
data = [[x.strip() for x in row.split('\t')] for row in text.split('\n') if row.strip()]
for row in data:
print(row)
结果与第一种方法相同。
答案 1 :(得分:1)
arr = list(list(s.strip() for s in row.split('\\t')) for row in str(proc[0]).split('\\r\\n'))