使用Python编写的csv文件包含错误的数字格式

时间:2013-11-25 21:46:49

标签: python csv numpy format writer

此脚本读取文本文件,获取每列的每3行的平均值并将其写入csv文件:

输入文件:

2013-08-29T15:11:18.55912   0.019494552 0.110042184 0.164076427 0.587849877
2013-08-29T15:11:18.65912   0.036270974 0.097213155 0.122628797 0.556928624
2013-08-29T15:11:18.75912   0.055350041 0.104121094 0.121641949 0.593113069
2013-08-29T15:11:18.85912   0.057159263 0.107410588 0.198122695 0.591797271
2013-08-29T15:11:18.95912   0.05288292  0.102476346 0.172958062 0.591139372
2013-08-29T15:11:19.05912   0.043507861 0.104121094 0.162102731 0.598376261
2013-08-29T15:11:19.15912   0.068343545 0.102805296 0.168517245 0.587849877
2013-08-29T15:11:19.25912   0.054527668 0.105765841 0.184306818 0.587191978
2013-08-29T15:11:19.35912   0.055678991 0.107739538 0.169997517 0.539165352

脚本:

data = loadtxt('infile.txt', usecols = (1,2,3,4))
with open ('out.csv', 'wb') as outfile:
    writer = csv.writer(outfile, delimiter = '\t')
    seg_len = 3       
    for x in range(0, len(data[:,1]), seg_len):
        sample_means = ([x/seg_len], [mean(data[x:x+seg_len,i]) for i in range(4)])
        bi = list(chain.from_iterable(sample_means))
        writer.writerow ((', '.join(map(repr, bi))))

输出CSV文件:

0    0.037038...    0.10379...  ...
1    0.051183...    0.10466...  ...
2    00059516...    0.10543...  ...

但是当我打开csv文件和sum列时,它给出了零!好像他们不是数字。我直接从CSV文件复制,它看起来像这样:

"0      "   "   0   .   0   3   7   0   3   8   5   2   2   3   3   3   3   3   3   3   3       "   "   0   .   1   0   3   7   9   2   1   4   4   3   3   3   3   3   3   3   2       "
"1      "   "   0   .   0   5   1   1   8   3   3   4   7   9   9   9   9   9   9   9   9   7       "   "   0   .   1   0   4   6   6   9   3   4   2   6   6   6   6   6   6   6   6       "
"2      "   "   0   .   0   5   9   5   1   6   7   3   4   6   6   6   6   6   6   6   6   1       "   "   0   .   1   0   5   4   3   6   8   9   1   6   6   6   6   6   6   6   6       "

有人可以建议如何解决这个问题吗?

2 个答案:

答案 0 :(得分:4)

也许你在找这个?

sample_means = [x/seg_len] + [mean(data[x:x+seg_len,i]) for i in range(4)]
writer.writerow(sample_means)

答案 1 :(得分:0)

您的部分问题是writerow需要一个可迭代的值。你给它一个字符串(', '.join的输出),所以字符串中的每个字母都在一个单独的分隔字段中写入文件。如果您将bi直接传递给writerow,它至少应解决该特定问题。