所以,我编写了一个小测试代码,用于计算文件夹中所有csv文件的平均值。
parser = argparse.ArgumentParser(description='avg data from csv to csv')
parser.add_argument('--input', required = True)
parser.add_argument('--output', required = True)
args = parser.parse_args()
# Get command arguments
input_files_path = args.input
output = args.output
avg = []
for infile in glob.glob(input_files_path+"*.csv"):
a = np.loadtxt(infile, delimiter=",")
mean = np.mean(a, axis=0)
avg.append((mean))
print infile
np.savetxt(output,(avg), fmt = "%.1f", delimiter=", ")
但现在问题是,当我试图在csv文件中保存文件名时,如下所示:
parser = argparse.ArgumentParser(description='avg data from csv to csv')
parser.add_argument('--input', required = True)
parser.add_argument('--output', required = True)
args = parser.parse_args()
# Get command arguments
input_files_path = args.input
output = args.output
avg = []
for infile in glob.glob(input_files_path+"*.csv"):
a = np.loadtxt(infile, delimiter=",")
mean = np.mean(a, axis=0)
avg.append((infile,(mean)))
print infile
np.savetxt(output,(avg), fmt = "%.1f", delimiter=", ")
它会抛出错误:
np.savetxt(output,(avg), fmt = "%.1f", delimiter=", ")
File "/usr/lib/python2.7/dist-packages/numpy/lib/npyio.py", line 963, in savetxt
X = np.asarray(X)
File "/usr/lib/python2.7/dist-packages/numpy/core/numeric.py", line 235, in asarray
return array(a, dtype, copy=False, order=order)
ValueError: cannot set an array element with a sequence
我似乎不明白这是什么问题。也许是因为我试图将字符串附加到浮点数?我检查了fmt
s numpy ..但我没有找到任何有趣的东西。
/usr/lib/python2.7/dist-packages/numpy/lib/npyio.py:773: UserWarning: loadtxt: Empty input file: "../robalo-31.1/op.csv"
warnings.warn('loadtxt: Empty input file: "%s"' % fname)
/usr/lib/python2.7/dist-packages/numpy/core/fromnumeric.py:2374: RuntimeWarning: invalid value encountered in double_scalars
return mean(axis, dtype, out)
答案 0 :(得分:1)
我猜你使用np.savetxt
提供的格式字符存在问题。 (你有一个字符串浮点元组,你的分隔符意味着你只使用浮点数)。我建议csv
代替:
>>> import csv
>>> import StringIO
>>> g = StringIO.StringIO()
>>> writer = csv.writer(g,delimiter=',') #this is the part that is applicable
>>> writer.writerows(lst)
>>> g.seek(0)
>>> print g.read()
foo,2
bar,4
所以我猜你的代码可能很简单:
with open(output,'wb') as fout:
writer = csv.writer(fout,delimiter=',')
writer.writerows(avg)
而不是
np.savetxt(output,(avg), fmt = "%.1f", delimiter=", ")