如堆栈溢出所示,我正在使用 iter_loadtxt加载 > 600MB 的文本文件。有时,生成文件的程序会错误地写入文本文件和 iter_loadtxt barfs:
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "jpk_outfile_to_LOG_timeV4.py", line 296, in makeLOGtimefile
a=readjpk(filename,lateral,vertical,xpos)
File "jpk_outfile_to_LOG_timeV4.py", line 107, in readjpk
a=iter_loadtxt2( filename,delimiter=" ",skiprows=7)
File "jpk_outfile_to_LOG_timeV4.py", line 58, in iter_loadtxt2
data = np.fromiter(iter_func(), dtype=dtype)
File "jpk_outfile_to_LOG_timeV4.py", line 55, in iter_func
yield dtype(item)
*605.6998r: invalid literal for float(): -1.5005492E-5
我的解决方案是尝试添加一个替换 dtype 的异常,但这不起作用。有什么建议么?
#==============================================================================
# credit to joe kington on stackoverflow:
# https://stackoverflow.com/questions/8956832
#==============================================================================
def iter_loadtxt2(filename, delimiter=' ', skiprows=6, dtype=float):
def iter_func():
with open(filename, 'r') as infile:
for _ in range(skiprows):
next(infile)
for line in infile:
line = line.rstrip().split(delimiter)
for item in line:
# added try/except/else to this loop, originally only had yiled dtype(item)
try:
yield dtype(item)
a=dtype(item)
except:
print "fail"
else:
yield a
iter_loadtxt.rowlength = len(line)
data = np.fromiter(iter_func(), dtype=dtype)
data = data.reshape((-1, iter_loadtxt.rowlength))
return data