python列表转换为表格

标签 python list

我有一个列表 abc 如下。我想将其转换为表格,因为我需要将此数据写入文件(CSV 或任何其他分隔符),然后在 Excel 中打开该文件。

该表中的列将是 company、source、time、link、title 并且对于列表中的第一个元素,第一行将具有值 -

intel corporation, 
Motely Fool, 
4 hours ago,
http://www.fool.com/investing/general/2016/05/09/intel-corporations-strange-choice.aspx,
Intel Corporation's Strange Choice

我怎样才能达到同样的效果? abc 有数千个元素

abc=[{'intel corporation': {'source': u'Motley Fool', 'time': u'4 hours ago', 'link': u'http://www.fool.com/investing/general/2016/05/09/intel-corporations-strange-choice.aspx', 'title': u"Intel Corporation's Strange Choice"}}, {'intel corporation': {'source': u'Seeking Alpha', 'time': u'33 minutes ago', 'link': u'http://seekingalpha.com/article/3973271-intels-augmented-reality-plans-coming-focus', 'title': u"Intel's Augmented Reality Plans Are Coming Into Focus"}}, {'intel corporation': {'source': u'Seeking Alpha', 'time': u'7 hours ago', 'link': u'http://seekingalpha.com/article/3973098-intel-consider-debt-large-buyback', 'title': u'Should Intel Consider More Debt For A Large Buyback?'}}, {'intel corporation': {'source': u'Schaeffers Research (blog)', 'time': u'9 hours ago', 'link': u'http://www.schaeffersresearch.com/content/news/2016/05/09/analyst-downgrades-apple-inc-berkshire-hathaway-inc-and-intel-corporation', 'title': u'Analyst Downgrades: Apple Inc., Berkshire Hathaway Inc., and Intel ...'}}, {'intel corporation': {'source': u'Money News (press release)', 'time': u'Money News (press release)-12 hours ago Explore in depth (11 more articles)', 'link': u'http://www.newsismoney.com/2016/05/09/shares-of-tech-companies-intel-corporation-nasdaqintc-microsoft-corporation-nasdaqmsft-news-update/', 'title': u'Shares of Tech Companies Intel Corporation (NASDAQ:INTC ...'}}, {'intel corporation': {'source': u'Motley Fool', 'time': u'May 6, 2016', 'link': u'http://www.fool.com/investing/general/2016/05/06/why-intel-corporation-quit-smartphones-to-focus-on.aspx', 'title': u'Why Intel Corporation Quit Smartphones to Focus on 5G'}}, {'intel corporation': {'source': u'Motley Fool', 'time': u'May 6, 2016', 'link': u'http://www.fool.com/investing/general/2016/05/06/dont-blame-x86-for-intel-corporations-smartphone-f.aspx', 'title': u"Don't Blame X86 for Intel Corporation's Smartphone Failure"}}, {'intel corporation': {'source': u'Street Updates', 'time': u'11 hours ago', 'link': u'http://www.streetupdates.com/2016/05/09/valuable-analysts-trends-to-observe-intel-corporation-nasdaqintc-taiwan-semiconductor-manufacturing-company-ltd-nysetsm/', 'title': u'Valuable Analysts Trends to Observe: Intel Corporation (NASDAQ ...'}}, {'intel corporation': {'source': u'Motley Fool', 'time': u'May 5, 2016', 'link': u'http://www.fool.com/investing/general/2016/05/05/intel-corporation-its-time-to-replace-ceo-brian-kr.aspx', 'title': u"Intel Corporation: It's Time to Replace CEO Brian Krzanich"}}, {'intel corporation': {'source': u'Amigobulls', 'time': u'Amigobulls-May 5, 2016 Explore in depth (10 more articles)', 'link': u'http://amigobulls.com/articles/intel-corporations-mobile-exit-could-drive-long-term-gains', 'title': u"Intel Corporation's Mobile Exit Could Drive Long-Term Gains"}}, {'intel corporation': {'source': u'Seneca Globe', 'time': u'7 hours ago', 'link': u'http://www.senecaglobe.com/intel-corporation-nasdaqintc-not-great-time-continues-downward-trend-trina-solar-nysetsl/323986/', 'title': u'Intel Corporation (NASDAQ:INTC) Has Not Been Having A Great ...'}}, {'intel corporation': {'source': u'The News Journal', 'time': u'19 minutes ago', 'link': u'http://news4j.com/ruling-stocks-in-todays-market-intel-corporation-nasdaqintc-5/', 'title': u"Ruling stocks in today's market: Intel Corporation (NASDAQ:INTC)"}}]

最佳答案

import csv
import sys    # sys.setdefaultencoding is cancelled by site.py
reload(sys)    # to re-enable sys.setdefaultencoding()
sys.setdefaultencoding('utf-8')

with open('data.csv', 'wb') as out:
    writer = csv.DictWriter(out, ['company', 'source', 'time', 'link', 'title'])
    writer.writeheader()

    for thing in abc:
        for company, details in thing.items():
            details['company'] = company
            writer.writerow(details)

关于python列表转换为表格,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37126617/

相关文章:

python - 在没有 collections.counter 的情况下根据整数值对字典进行排序

java - 获取随机列作为字符串列表

python - 在 sharedmods 上的 Debian Jessie 段错误上编译 Python 3.6.2

python - Pandas 将一天添加到列

python - Django 2.0 中 "django.core.xheaders"的替代 Django 包

python - 在 Django 中保存 ModelForm 后获取主键

javascript - JS : How to update list items positions based on a new order/"list"?

java - 如果 ArrayList 对象的字段之一包含重复条目,如何修改该对象?

java - 在 Java 中合并和删除多个列表中的重复项的最佳方法

python - 试验 python 线程,如何停止线程