2016-05-09 107 views
2

我有一個列表abc如下。我想將其轉換爲表格,因爲我需要將此數據寫入文件(CSV或任何其他分隔符),然後在Excel中打開該文件。python list convert to a table

在該表中的列將是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)"}}] 
+0

CSV或任何形式的表,我可以在Excel中打開 – user2543622

+0

'Pandas'是這種工作的一個非常有用的庫,但沒有額外的信息來回答這個問題很難。特別是,它有很好的I/O方法來將數據幀轉換爲csv和許多其他格式(所有格式都很容易命名爲'.to_csv')。你的數據看起來如何? – jdg

回答

5
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) 
+0

任何想法,爲什麼我得到替代空白行?我還得到了'UnicodeEncodeError'' UnicodeEncodeError:'ascii'編解碼器無法編碼字符u'\ xf3'在位置7:序號不在範圍內(128)'任何想法如何解決它? - 我有公司名稱的多個值,例如「intel corporation」,「intel corporation10」,「intel corporation20」,「intel corporation30」等。 – user2543622

+1

我將Alex的文件代碼更改爲'wb',以處理交替空白行。你有一個unicode問題,這就是爲什麼你有ascii問題 - 這裏有一些關於這個問題的問題 – PyNEwbie

+0

我只需要替換空行的幫助。我能夠解決其他錯誤問題,使用'http:// stackoverflow.com/questions/13485546 /轉換 - unicode -in-in-python' – user2543622

1

我假設你正在談論像Excel電子表格的表。如果是這樣,那麼你可以看看熊貓包http://pandas.pydata.org/

安裝包後,你可以遍歷列表中的項目,然後迭代地將它們插入熊貓數據框(請參閱熊貓鏈接)。另外看看Convert Python dict into a dataframe它有一些信息可以幫助你。

如果您需要,可以將Dataframe對象導出爲.xlsx或csv格式。

+0

我試過'pd.DataFrame(abc)',但它沒有以我想要的方式創建表格。該表只有一列名爲「英特爾公司」。但我希望這是一個列,並且表中只有一列需要分成4個不同的列 – user2543622

+0

您需要將列表中的元素迭代添加到數據框中。 pd.DataFrame(abc)適用於字典而不是列表。 HTH。 –