我想通过添加额外的列扩展这个工作代码..下面给出的示例文件..现在的问题是,我需要将框括号[]中的所有项目作为一列,如总列数为6如何写出条件继续下去?
Sample.txt的
7.020000 993.000000 755.000000 FAIL 3 [ 62 02 01 23 26 30 35 C0 C0 C0 C0 ]
7.020000 993.000000 803.000000 FAIL 5 [ 62 02 01 23 26 30 35 C0 C0 C0 C0 ]
7.020000 993.000000 811.000000 FAIL 2 [ 62 02 01 23 26 30 35 C0 C0 C0 C0 ]
7.020000 993.000000 828.000000 PASS 1 []
7.020000 993.000000 876.000000 PASS 6 []
7.020000 993.000000 894.000000 FAIL 3 [ 62 02 01 23 26 30 35 C0 C0 C0 C0 ]
7.020000 993.000000 913.000000 FAIL 5 [ 62 02 01 23 26 30 35 C0 C0 C0 C0 ]
7.020000 993.000000 954.000000 FAIL 2 [ 62 02 01 23 26 30 35 C0 C0 C0 C0 ]
任何建议都会有所帮助!!!!!!!!
import matplotlib.pyplot as plt
from mpl_toolkits.mplot3d import Axes3D
import numpy as np
fig = plt.figure(figsize=(20, 20))
fig.savefig('Test2.png')#, dpi=600)
ax = Axes3D(fig) # ax = fig.add_subplot(111, projection='3d')
ax.set_title("Plot 3d",fontsize=14)
ax.set_xlabel('Voltage (V)', fontsize=12)
ax.set_ylabel('Pulse Delay(ms)', fontsize=12)
ax.set_zlabel('Pulse Width(ms)', fontsize=12)
ax.grid(True, linestyle='-', color='0.75')
data = np.genfromtxt('./Sample.txt', dtype=[('col1', 'f8'), ('col2', 'i16'), ('col3', 'i16'), ('col4', 'S15'), ('col5', 'i16'), ('col6', 'S24')])
m = data["col4"]
data1 = data[m == "PASS"]
data2 = data[m != "PASS"]
for dat, color in [(data1, 'g'), (data2, 'r')]:
# Don't forget that having empty data columns may raise exceptions ...
try:
x, y, z = dat['col1'], dat['col2'], dat['col3']
ax.scatter(xs=x, ys=y, zs=z, s=50, c=color, marker='o', linewidths=0)
except:
pass
plt.show()
答案 0 :(得分:0)
看起来像正则表达式的工作:
data = np.fromregex('./Sample.txt',
r'(\d+\.\d+)\s+(\d+)\.\d+\s+(\d+)\.\d+\s+(FAIL|PASS)\s+(\d+)\s+\[\s*(.*?)\s*\]',
dtype=[('col1', 'f8'), ('col2', 'i16'), ('col3', 'i16'),
('col4', 'S15'), ('col5', 'i16'), ('col6', 'S24')])
答案 1 :(得分:0)
建议使用delimiter
指定列宽,这似乎与您保持一致:
data = np.genfromtxt('./temp.dat', dtype=[('col1', 'f8'), ('col2', 'i16'),
('col3', 'i16'), ('col4', 'S15'), ('col5', 'i16'), ('col6','S37')],
delimiter = (10,13,13,4,7,40))
为第0行生成:
(7.02, 993, 755, 'FAIL', 3, ' [ 62 02 01 23 26 30 35 C0 C0 C0 C0 ]')
您可能需要进一步剥离字符串列,然后根据需要处理类似数组的值。
如果你不能依赖列宽,你可能会被迫逐行搜索'['和']'。