【发布时间】:2021-03-23 16:36:26
【问题描述】:
我有相当大的 LZMA 压缩数据文件,我想使用 pandas 读取这些文件以提取某些列的最小值和最大值。该文件是使用在 MPI 下运行的程序的日志文件中的 grep -n 生成的,因此包含多个 MPI 等级同时写入标准输出的乱码行。
这个问题与this one 非常相似,但我需要为每列做三次相同的事情。我已经尝试了那里提供的各种答案,但无济于事。
这是我目前得到的 Python 脚本:
import os # to check file existence
import sys # for argc, argv
import re # regex
import lzma as xz
import numpy as np
import pandas as pd
# Quick exit if file does not exist
if not os.path.exists(argv[1]):
help(); sys.exit( 'Error: cannot read file', argv[1] );
else:
# Define column names and columns to take
cols = [ 4, 7, 10 ];
colnames = [ 'm', 'n', 'k' ];
# Read file through LZMA decompressor
ifname = argv[1];
ifile = xz.open( ifname, 'rt' );
data = pd.read_csv( ifile, delim_whitespace=True, \
usecols=cols, names=colnames, \
error_bad_lines=False );
ifile.close();
### Insert filtering method here to transform data to data_clean
mdims = data_clean['m'].to_numpy();
mmin = np.amin(mdims);
mmax = np.amax(mdims);
ndims = data_clean['n'].to_numpy();
nmin = np.amin(ndims);
nmax = np.amax(ndims);
kdims = data_clean['k'].to_numpy();
kmin = np.amin(kdims);
kmax = np.amax(kdims);
# Display output
print( re.sub( ifname, '.xz', '' ), ':' );
print( 'M =', mmin, '-', mmax );
print( 'N =', nmin, '-', nmax );
print( 'K =', kmin, '-', kmax );
sys.exit(0);
Here 是您可以使用的两个数据文件进行测试。任何帮助将不胜感激。
【问题讨论】: