简体   繁体   English

如何使用 numpy 以行和列形式将数据保存在.csv 文件中

[英]How to save data in .csv file in row and column form using numpy

I am trying to read and image using OpenCV and after reading that image I have got some data which I have to save in a CSV file using numpy.我正在尝试使用 OpenCV 读取和图像,在读取该图像后,我得到了一些数据,我必须使用 numpy 将这些数据保存在 CSV 文件中。 Here is the program:-这是程序: -

import cv2 as cv
import numpy as np
import os

img1 = cv.imread('C:/Users/sbans/Pictures/bird.jpg')
dataA1 = os.path.basename('C:/Users/sbans/Pictures/bird.jpg')
height, width, channels = img1.shape
dataA2 = height
dataA3 = width
dataA4 = channels
a = int(height/2)
b = int(width/2)
px1  = img1[a,b]
dataA5 = px1[0]
dataA6 = px1[1]
dataA7 = px1[2]
a = np.array([dataA1, dataA2, dataA3, dataA4, dataA5, dataA6, dataA7])

img2 = cv.imread('C:/Users/sbans/Pictures/cat.jpg')
dataB1 = os.path.basename('C:/Users/sbans/Pictures/cat.jpg')
height, width, channels = img2.shape
dataB2 = height
dataB3 = width
dataB4 = channels
a = int(height/2)
b = int(width/2)
px2 = img2[a,b]
dataB5 = px2[0]
dataB6 = px2[1]
dataB7 = px2[2]
b = np.array([dataB1, dataB2, dataB3, dataB4, dataB5, dataB6, dataB7])
np.savetxt("stats.csv", np.stack((a,b)), delimiter=",", fmt='%s')

This error is coming:-这个错误来了: -

Traceback (most recent call last):回溯(最近一次通话最后):

File "C:\Users\sbans\Documents\demo_opencv.py", line 32, in np.savetxt("stats.csv", np.stack((a,b)), delimiter=",", fmt='%s')文件“C:\Users\sbans\Documents\demo_opencv.py”,第 32 行,在 np.savetxt("stats.csv", np.stack((a,b)), delimiter=",", fmt=' %s')

File "< array_function internals>", line 6, in stack文件“< array_function internals>”,第 6 行,在堆栈中

File "C:\Users\sbans\AppData\Local\Programs\Python\Python37\lib\site-packages\numpy\core\shape_base.py", line 425, in stack文件“C:\Users\sbans\AppData\Local\Programs\Python\Python37\lib\site-packages\numpy\core\shape_base.py”,第 425 行,在堆栈中

raise ValueError('all input arrays must have the same shape') ValueError: all input arrays must have the same shape raise ValueError('all input arrays must have the same shape') ValueError: all input arrays must have the same shape

You could simplify the code a bit by defining a function您可以通过定义 function 来稍微简化代码

def get_array(file):
    img = cv.imread(file)
    basename = os.path.basename(file)
    height, width, channels = img.shape
    h = int(height/2)
    w = int(width/2)
    px  = img[h,w]
    return np.array([basename, height, width, channels, px[0], px[1], px[2]]) 

Then savetxt can accept a list of same-sized 1D arrays然后 savetxt 可以接受相同大小的一维 arrays 的列表

a = get_array('C:/Users/sbans/Pictures\bird.jpg')
b = get_array('C:/Users/sbans/Pictures\cat.jpg') 

np.savetxt("stats.csv", (a, b), delimiter=",", fmt='%s')

The default behavior of np.savetxt method is to replace the existing file with a new data. np.savetxt方法的默认行为是用新数据替换现有文件。

If you want to sequentially write the data to a file then you need to have a reference for that file then use it for np.savetxt .如果要按顺序将数据写入文件,则需要对该文件有一个引用,然后将其用于np.savetxt

For your case:对于您的情况:

f = open('stats.csv','w')

...
np.savetxt(f, np.row_stack(np.column_stack((dataA1, dataA2, dataA3, dataA4, dataA5, dataA6, dataA7))), delimiter=",", fmt='%s')


...
np.savetxt(f, np.row_stack(np.column_stack((dataB1, dataB2, dataB3, dataB4, dataB5, dataB6, dataB7))), delimiter=",", fmt='%s')


f.close()

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何在行而不是列中保存 CSV 文件 - How to save a CSV file in row not column 如何在 csv 文件中将一列元素保存为一行? - How to save a column of elements as a row, in a csv file? How can an array be added as a separate column to a CSV file using numpy (preferable) or python 3 without overwriting the CSV file or data? - How can an array be added as a separate column to a CSV file using numpy (preferable) or python 3 without overwriting the CSV file or data? 如何保存 .csv 文件,其中我使用 pandas 将字符串数据转换为 .csv 文件特定列的数字数据? - How do I save the .csv file where i converted the string data into numerical data of a specific column of a .csv file using pandas? 如何将numpy ndarray保存为.csv文件? - How to save numpy ndarray as .csv file? 使用 numpy 读取 csv 文件的一列 - Reading one column of a csv file using numpy 操作 CSV 文件中的行数据并使用 Python 将其附加到新列中 - Manipulating row data in CSV file and appending it in a new column using Python 将 numpy 数组作为 Pandas 中的列保存/加载到 csv 文件 - Save/Load numpy array as column in Pandas to csv file 如何在不使用numpy / pandas的情况下处理csv文件中的丢失数据? - How to handle missing data in a csv file without using numpy/pandas? 如何使用 python 在 csv 文件中逐行读取一列数据 - How to read one column data as one by one row in csv file using python
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM