有谁知道,如何从我试图使用numpy.genfromtxt读取的文本文件中跳过括号--我的数据文件的格式
1.466 ((5.68 3.3 45.7)(4.5 6.7 9.5))发布于 2015-06-26 02:13:49
np.genfromtxt可以接受迭代器:
import numpy as np
import re
with open('data', 'r') as f:
lines = (line.replace('(',' ').replace(')',' ') for line in f)
arr = np.genfromtxt(lines)
print(arr)收益率
[ 1.466 5.68 3.3 45.7 4.5 6.7 9.5 ]或者,您可以(在Python2中)使用str.translate或(在Python3中)使用bytes.translate方法,这要快一些:
import numpy as np
import re
try:
# Python2
import string
table = string.maketrans('()',' ')
except AttributeError:
# Python3
table = bytes.maketrans(b'()',b' ')
with open('data', 'rb') as f:
lines = (line.translate(table) for line in f)
arr = np.genfromtxt(lines)
print(arr)https://stackoverflow.com/questions/31063372
复制相似问题