字典到csv单独的列

时间:2019-01-31 18:42:29

标签: python python-3.x

我想将字典放入csv文件中。我想在单独的列中包含键和值。但是现在我以两行结尾:一行包含所有键,而一行包含值

dic={'Previous Close': '150.42', 'Open': '165.60', 'Bid': '168.81 x 900', 'Ask': '168.90 x 800', "Day's Range": '57,881,627', '52 Week Range': '25,593,803', 'Volume': '482.308B', 'Avg. Volume': '0.65', 'Market Cap': '25.29', 'Beta (3Y Monthly)': '6.64', 'PE Ratio (TTM)': 'N/A', 'EPS (TTM)': '184.67', 'Earnings Date': '339.89', 'Forward Dividend & Yield': '308.42', 'Ex-Dividend Date': '1,110.19', '1y Target Est': '1,717.32'}

df=pd.DataFrame([(k,v) for k,v in dic.items()],columns=["key","value"] )
df.to_csv("test2.csv",sep=",")

3 个答案:

答案 0 :(得分:1)

您的数字中有,个千位分隔符-如果将,作为csv分隔符,将很难再次加载它-我删除了它们。

无熊猫解决方案:

dic={'Previous Close': '150.42', 'Open': '165.60', 'Bid': '168.81 x 900', 
     'Ask': '168.90 x 800', "Day's Range": '57,881,627', '52 Week Range': '25,593,803', 
     'Volume':'482.308B', 'Avg. Volume': '0.65', 'Market Cap': '25.29', 
     'Beta (3Y Monthly)': '6.64', 'PE Ratio (TTM)': 'N/A', 'EPS (TTM)': '184.67', 
     'Earnings Date': '339.89', 'Forward Dividend & Yield': '308.42', 
     'Ex-Dividend Date': '1,110.19', '1y Target Est': '1,717.32'}


with open ("t.csv","w") as f:
    f.write("key,value\n")
    for k,v in dic.items():
        f.write(f"{k},{v.replace(',','')}\n")

print(open("t.csv").read())

输出(从值中删除,以使重新加载更加容易):

key,value
Previous Close,150.42
Open,165.60
Bid,168.81 x 900
Ask,168.90 x 800
Day's Range,57881627
52 Week Range,25593803
Volume,482.308B
Avg. Volume,0.65
Market Cap,25.29
Beta (3Y Monthly),6.64
PE Ratio (TTM),N/A
EPS (TTM),184.67
Earnings Date,339.89
Forward Dividend & Yield,308.42
Ex-Dividend Date,1110.19
1y Target Est,1717.32

答案 1 :(得分:0)

我认为这就是您想要的...

dic={'Previous Close': '150.42', 'Open': '165.60', 'Bid': '168.81 x 900', 'Ask': '168.90 x 800', "Day's Range": '57,881,627', '52 Week Range': '25,593,803', 'Volume': '482.308B', 'Avg. Volume': '0.65', 'Market Cap': '25.29', 'Beta (3Y Monthly)': '6.64', 'PE Ratio (TTM)': 'N/A', 'EPS (TTM)': '184.67', 'Earnings Date': '339.89', 'Forward Dividend & Yield': '308.42', 'Ex-Dividend Date': '1,110.19', '1y Target Est': '1,717.32'}

df=pd.DataFrame([[k,v] for k,v in dic.items()],columns=["key","value"] )
df.to_csv("test2.csv",sep=",")

这是我得到的输出

key,value
0,Previous Close,150.42
1,Open,165.60
2,Bid,168.81 x 900
3,Ask,168.90 x 800
4,Day's Range,"57,881,627"
5,52 Week Range,"25,593,803"
6,Volume,482.308B
7,Avg. Volume,0.65
8,Market Cap,25.29
9,Beta (3Y Monthly),6.64
10,PE Ratio (TTM),N/A
11,EPS (TTM),184.67
12,Earnings Date,339.89
13,Forward Dividend & Yield,308.42
14,Ex-Dividend Date,"1,110.19"
15,1y Target Est,"1,717.32"

答案 2 :(得分:0)

如果您使用module csv,则可以指定一个引号字符并为所有字段加引号:

dic={'Previous Close': '150.42', 'Open': '165.60', 'Bid': '168.81 x 900', 
     'Ask': '168.90 x 800', "Day's Range": '57,881,627', '52 Week Range': '25,593,803', 
     'Volume':'482.308B', 'Avg. Volume': '0.65', 'Market Cap': '25.29', 
     'Beta (3Y Monthly)': '6.64', 'PE Ratio (TTM)': 'N/A', 'EPS (TTM)': '184.67', 
     'Earnings Date': '339.89', 'Forward Dividend & Yield': '308.42', 
     'Ex-Dividend Date': '1,110.19', '1y Target Est': '1,717.32'}

import csv
with open ("t.csv", "w", newline="") as f: 

    # quote all and use tab as quotechar - makes it easier when using excel
    w = csv.writer(f, delimiter="\t", quotechar='"', quoting=csv.QUOTE_ALL)
    w.writerow(["key","value"])
    w.writerows( dic.items() )

print(open("t.csv").read())

输出(此处的制表符看起来像是间隔不同的空格):

"key"   "value"
"Previous Close"    "150.42"
"Open"  "165.60"
"Bid"   "168.81 x 900"
"Ask"   "168.90 x 800"
"Day's Range"   "57,881,627"
"52 Week Range" "25,593,803"
"Volume"    "482.308B"
"Avg. Volume"   "0.65"
"Market Cap"    "25.29"
"Beta (3Y Monthly)" "6.64"
"PE Ratio (TTM)"    "N/A"
"EPS (TTM)" "184.67"
"Earnings Date" "339.89"
"Forward Dividend & Yield"  "308.42"
"Ex-Dividend Date"  "1,110.19"
"1y Target Est" "1,717.32"