numpy--读取CSV,一维数组转二维数组并写入Excel文件中

目录

整体功能:

一、读CSV文件

二、写入Excel中


整体功能:

        利用python的numpy框架,将CSV文件中的数据读取出来,整合到二维数组中,并写入Excel文件中。

习惯性先吐槽一下:

        由于本菜鸟是java工程师,很久没写python了,最近帮忙写个接口,顺便学习学习。没用过numpy,查了很久,网上乱七八糟的资料,真难。。。。最后看了reshape源码,将一维数组直接reshape,居然可以用,nice!上源码!

# not deprecated --- copy if necessary, view otherwise
@array_function_dispatch(_reshape_dispatcher)
def reshape(a, newshape, order='C'):
    """
    Gives a new shape to an array without changing its data.

    Parameters
    ----------
    a : array_like
        Array to be reshaped.
    newshape : int or tuple of ints
        The new shape should be compatible with the original shape. If
        an integer, then the result will be a 1-D array of that length.
        One shape dimension can be -1. In this case, the value is
        inferred from the length of the array and remaining dimensions.
    order : {'C', 'F', 'A'}, optional
        Read the elements of `a` using this index order, and place the
        elements into the reshaped array using this index order.  'C'
        means to read / write the elements using C-like index order,
        with the last axis index changing fastest, back to the first
        axis index changing slowest. 'F' means to read / write the
        elements using Fortran-like index order, with the first index
        changing fastest, and the last index changing slowest. Note that
        the 'C' and 'F' options take no account of the memory layout of
        the underlying array, and only refer to the order of indexing.
        'A' means to read / write the elements in Fortran-like index
        order if `a` is Fortran *contiguous* in memory, C-like order
        otherwise.

    Returns
    -------
    reshaped_array : ndarray
        This will be a new view object if possible; otherwise, it will
        be a copy.  Note there is no guarantee of the *memory layout* (C- or
        Fortran- contiguous) of the returned array.

一、读CSV文件

def print_hi(name):
    a = []
    i: int = 0
    with open('999.csv', encoding='utf-8-sig') as f:
        for row in csv.reader(f, skipinitialspace=True):
            i = i + 1
            for j in row[0:len(row) - 1]:   //舍弃CSV文件最后一列数据
                a.append(j)
    b = geek.reshape(a, [-1, 75])   //将一维数组转二维数组
    f.close()
    writeToExcel(b)                //写入到Excel文件中
    print(b)

二、写入Excel中

        2.1导入操作Excel的依赖

def writeToExcel(data_list):  //data_list要写入的二维数组
    # 定义一个workbook(文件),编码为utf-8
    workbook = xlwt.Workbook(encoding='utf-8')

    # 增加一个表单,cell_overwrite_ok=True表示可以覆盖原单元格中数据,默认为False,覆盖时会抛出异常
    sheet = workbook.add_sheet("sheet1", cell_overwrite_ok=True)

    # row代表行,col代表列
    row = 0
    col = 0

    # num_row代表行数,num_col代表列数
    num_row = 0
    num_col = 0

    # 将数据写入对应的位置
    for data in data_list:
        for i in data:
            sheet.write(num_row, num_col, i)
            num_col += 1
        num_row += 1
        num_col = 0

    # 保存数据到对应的excel中
    workbook.save("demo1.xls")

 使用前需要导包:

import numpy as geek

网上的例子:

定义一个大小12的一维数组,再reshape转换成3行4列的矩阵。

arr = geek.arange(12).reshape(3, 4)
    print("2D arr : \n", arr)
    print("Shape : ", arr.shape)

相同原理:自定义一个大小为6的一维数组 ,再转换。

    b = [1, 2, 3, 4, 5, 7]
    arr = geek.reshape(b, [2, 3])
    print("1D arr : \n", arr)
    print("Shape : ", arr.shape)

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值