惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

腾讯CDC
Microsoft Azure Blog
Microsoft Azure Blog
L
LangChain Blog
Y
Y Combinator Blog
Microsoft Security Blog
Microsoft Security Blog
宝玉的分享
宝玉的分享
B
Blog RSS Feed
MongoDB | Blog
MongoDB | Blog
Jina AI
Jina AI
D
Docker
B
Blog
Engineering at Meta
Engineering at Meta
Last Week in AI
Last Week in AI
OSCHINA 社区最新新闻
OSCHINA 社区最新新闻
I
InfoQ
G
Google Developers Blog
博客园 - Franky
freeCodeCamp Programming Tutorials: Python, JavaScript, Git & More
让小产品的独立变现更简单 - ezindie.com
让小产品的独立变现更简单 - ezindie.com
The GitHub Blog
The GitHub Blog
T
The Blog of Author Tim Ferriss
大猫的无限游戏
大猫的无限游戏
阮一峰的网络日志
阮一峰的网络日志
U
Unit 42

陈少文的网站

巨变与机遇的未来十年 Kubernetes 平台管理软件压力测试方案 使用镜像部署 Hexo 静态页面 终于等到你 - GitHub 镜像仓库服务(ghcr.io) 一起来学 Go --(6)Interface 一起来学 Go --(5)Goroutine 和 Channel 什么是函数式编程 如何在 Kubernetes 集群集成 Kata 柯里化与偏函数 使用 PyGithub 自动创建 Label 软件产品是团队能力的输出 Helm 2 、Helm 3 比较 IoT 变现 Kubernetes 中的 DNS 服务 国内的 Helm 镜像源 Harbor 使用自签证书支持 Https 访问 DevOps 工具链之 Prow 如何使用 kfctl 安装 Kubeflow VS Code 无法下载 Go 插件的工具包 工程师更应具有服务精神 你不知道的 Docker 使用技巧 使用 Docker 运行 Tensorflow 论中国 什么是左移 如何清空 Git 仓库全部历史记录 一禅小和尚 有风吹过厨房 时间的玫瑰 如何在 CentOS 安装 GPU 驱动 开发 Tips(19)
Python读写Excel
微信公众号 · 2017-04-16 · via 陈少文的网站
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
# coding=utf-8
def list_wirte_to_excel(data_list):
    '''
    :param data_list = [(1, 2, 3),(11, 21, 31)]:
    '''
    import xlwt
    excel = xlwt.Workbook(encoding='utf-8')
    sheet1 = excel.add_sheet(u'sheet1', cell_overwrite_ok=True)  # 创建sheet1
    columns = [u'第一列', u'第二列', u'时间']

    # 创建列名栏
    for i in xrange(0, len(columns)):
        sheet1.write(0, i, columns[i])

    # 写入数据
    for i in xrange(0, len(data_list)):
        if len(data_list[i]) == len(columns):
            # write(行,列,数据,样式)
            sheet1.write(i + 1, 0, data_list[i][0])
            sheet1.write(i + 1, 1, data_list[i][1])
            sheet1.write(i + 1, 2, data_list[i][2])
    excel.save('excel.xls')


def excel_to_list(excel_path):
    '''
    :param excel_path 能访问的excel路径:
    :return包含全部数据的list:[(第一列数据), (第二列数据)]
    '''
    import xlrd
    wb = xlrd.open_workbook(excel_path)
    # 两种方式:索引和名字
    sheet = wb.sheet_by_index(0)

    data = [sheet.row_values(rownum) for rownum in xrange(sheet.nrows)]

    # 如果只想返回第一列数据:
    # sheet.col_values(0)
    # 通过索引读取数据
    # cell(行,列), 获取第一行,第一列数据
    # sheet.cell(0, 0).value
    return data[1:]

if __name__ == '__main__':
    import random
    import datetime
    data = [(random.randint(0, 1000), random.randint(0, 1000), datetime.datetime.now().strftime('%Y-%m'))
            for i in xrange(1000)]
    list_wirte_to_excel(data)

    print excel_to_list('./excel.xls')