Python:用 pandas 将numpy数据写入excel

#!/usr/bin/env python
# -*- coding: utf-8 -*-
#
import openpyxl
import numpy as np
import pandas as pd

def ColorsSave(colours,name):
        data = pd.DataFrame(colours)
        writer = pd.ExcelWriter("F:/find_color/texture images result./"+ name + ".xlsx")
        data.to_excel(writer, "page_1", float_format = '%d')
        writer.save()
        writer.close()

这只是一个函数而已。需要记录的是:

  1. 如何存入指定路径下:
    可以理解为,写到指定路径就是存入一个指定的字符串,那需要做的就是把地址链接成一个合理的字符串,比如函数中 name 是个变量,那把这个保留,两端的作为字符串各自加双引号,用 + 链接即可。存image,txt等都是相同的道理。

  2. 关于指定路径的斜杠问题:
    当我们直接复制电脑中的路径的时候是这样的:
    F:\find_color\texture images result
    但是用的时候要将\换成\\,即:

"F:\\find_color\\texture images result.temp-excel.xlsx"

或:

r"F:\find_color\texture images result.temp-excel.xlsx"
# the 'r' prefix means raw string

或把\改成/

"F:/find_color/texture images result.temp-excel.xlsx"
  1. float_format=’%d’ 存入的是整数格式。
  2. float_format=’%.5f’ 存入的是小数格式,小数点后可有5位。
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值