[英]float to integer in csv.writer python
I have different files (at least 10) with a 34x34 matrix and i sum each file into a single 34x34 matrix. 我有不同的文件(至少10个)和34x34矩阵,我将每个文件加到一个34x34矩阵中。 Here is my code:
这是我的代码:
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()
The code creates a folder in the direction of the original files, but the resulting file gives me the values in floats and i need integers. 代码在原始文件的方向创建一个文件夹,但生成的文件给了我浮点数的值,我需要整数。
您应该将a = np.zeros((34,34))
更改为a = np.zeros((34,34), dtype=int)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.