V2EX = way to explore
V2EX 是一个关于分享和探索的地方
现在注册
已注册用户请  登录
推荐学习书目
Learn Python the Hard Way
Python Sites
PyPI - Python Package Index
http://diveintopython.org/toc/index.html
Pocoo
值得关注的项目
PyPy
Celery
Jinja2
Read the Docs
gevent
pyenv
virtualenv
Stackless Python
Beautiful Soup
结巴中文分词
Green Unicorn
Sentry
Shovel
Pyflakes
pytest
Python 编程
pep8 Checker
Styles
PEP 8
Google Python Style Guide
Code Style from The Hitchhiker's Guide
zcsnbb
V2EX  ›  Python

列表怎么保存到 csv 文件能用 excel 打开啊

  •  
  •   zcsnbb · 2018-09-27 21:29:18 +08:00 · 1962 次点击
    这是一个创建于 2008 天前的主题,其中的信息可能已经有所发展或是发生改变。

    lst = [{"a": 1, "b": 1}, {"a": 2, "b": 2},{"a": 3, "b": 3},{"a": 3, "b": 3}] 比如这样

    4 条回复    2018-09-28 10:33:27 +08:00
    liwufan
        1
    liwufan  
       2018-09-27 21:33:10 +08:00
    fangdingjun
        2
    fangdingjun  
       2018-09-27 21:34:33 +08:00 via Android
    英文逗号分隔,中文要编码成 gbk
    xpresslink
        3
    xpresslink  
       2018-09-27 22:54:51 +08:00
    >>> import csv
    >>> f = open('d:/tmp/test.csv', 'w', encoding='gbk')
    >>> writer = csv.DictWriter(f, fieldnames=['a','b'])
    >>> for dic in [{"a": 1, "b": 1}, {"a": 2, "b": 2},{"a": 3, "b": 3},{"a": 3, "b": 3}]: writer.writerow(dic)

    5
    5
    5
    5
    >>> f.close()
    >>>
    lazyload
        4
    lazyload  
       2018-09-28 10:33:27 +08:00
    import pandas as pd
    from pandas import DataFrame
    lst = DataFrame([{"a": 1, "b": 1}, {"a": 2, "b": 2},{"a": 3, "b": 3},{"a": 3, "b": 3}])
    lst.to_csv('test.csv')
    关于   ·   帮助文档   ·   博客   ·   API   ·   FAQ   ·   我们的愿景   ·   实用小工具   ·   3801 人在线   最高记录 6543   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 27ms · UTC 10:35 · PVG 18:35 · LAX 03:35 · JFK 06:35
    Developed with CodeLauncher
    ♥ Do have faith in what you're doing.