在csv.writer python中浮点数为整数

时间:2015-03-05 22:27:59

标签: python csv matrix

我有不同的文件(至少10个)和34x34矩阵,我将每个文件加到一个34x34矩阵中。这是我的代码:

import csv
import numpy as np 
import os
import glob


a = np.zeros((34,34))
os.chdir("\dir\folder")     
events = glob.glob("*.txt")     

for file in events:
    d = np.loadtxt(file,delimiter=' ',dtype=int)
    a = a + d    
    path = "TotalEvents"

if not os.path.exists(path):                        
    os.makedirs(path, 0777)

SumMat = "TotalEvents" + ".txt"                         
with open(os.path.join("TotalEvents",SumMat), 'wb') as outfile: 
    writer = csv.writer(outfile, delimiter=" ", quoting=csv.QUOTE_NONE)
    writer.writerows(a)                         

outfile.close()

代码在原始文件的方向创建一个文件夹,但生成的文件给了我浮点数的值,我需要整数。

1 个答案:

答案 0 :(得分:0)

您应该将a = np.zeros((34,34))更改为a = np.zeros((34,34), dtype=int)