簡體   English   中英

寫入.csv超過一欄

[英]Writing to .csv more than one column

我有以下代碼:

#//Running my argument to check if the item in list is in dictionary and returning    this + corresponding value in dictionary//#
for key in n:
    if DICT.get(key):
        print ((key) + ' : ' + DICT[key])
    else:
        print((key) + ' : ' + "Not Available")

#Writing to .cvs file       
with open('test_write.csv', 'w') as fp:
    a = csv.writer(fp)
    a.writerows(n)

我想將上述for循環中的結果寫入.csv。
目前,我在分開的列中獲得了代碼第一部分的每個字母。
我需要在每列中都包含兩個。
我建議我需要將for循環轉換為dict? 但是我可能是錯的...

關於如何最簡單地執行此操作的任何想法?

編輯:
使用您的建議:

    with open('test_write.csv', 'w') as fp:
        a = csv.writer(fp)
        for key in n:
        if DICT.get(key):
            print ((key) + ' : ' + DICT[key])
            a.writerow(n)
        else:
            print((key) + ' : ' + "Not Available")
            a.writerow(DICT.get(n,"Not Available") for name in n)

我沒有得到我所期望的
我得到清單n的4行。 沒有表明DICT的價值。 我究竟做錯了什么...

writerows列出了可迭代對象。 嘗試使用writerow 從外觀上看, n是一個字典,因此要獲得一行標題和一行值,請執行以下操作:

a.writerow(n.keys())
a.writerow(n.values())

可以為第一行編寫a.writerow(n) ,但我希望更明確地顯示這一點。

還有一些添加所有默認值的快捷方式:

names = ['list','of','all','expected','keys']
data = {'list':'A', 'of':'zoo', 'keys':'foo'}
default = dict.fromkeys(names,"Not Available")
default.update(data)
data = default

留下內容的數據:

{'all': 'Not Available', 'of': 'zoo', 'list': 'A', 'expected': 'Not Available', 'keys': 'foo'}

編輯

給定DICT = {1:a,2:b,3:c,4:d}並且列表n = [1、2、5、6],只需執行以下操作:

a.writerow(n)
a.writerow(DICT.get(name,"Not Available") for name in n)

會在您的CSV文件中打印兩行,一行的鍵名位於n中,另一行的鍵名來自DICT,如果DICT中沒有特定的鍵,則顯示“不可用”字樣。

編輯2

您太努力了-DICT.get將照顧是否存在條目:

with open('test_write.csv', 'w') as fp:
    a = csv.writer(fp)
    a.writerow(n)
    a.writerow(DICT.get(name,"Not Available") for name in n)

這是相同的代碼,但形式更為冗長:

with open('test_write.csv', 'w') as fp:
    a = csv.writer(fp)
    # write row of header names
    a.writerow(n)

    # build up a list of the values in DICT corresponding to the keys in n
    values = []
    for name in n:
        if name in DICT:
            values.append(DICT[name])
        else:
            values.append("Not Available")
    # or written as a list comprehension:
    # values = [DICT[name] if name in DICT else "Not Available" for name in n]
    # 
    # or just use DICT.get, which does the name checking for us, and chooses the 
    # default value if the key is not found
    # values = [DICT.get(name, "Not Available") for name in n]

    # now write them out
    a.writerow(values)

    # or finally, the list build and writerow call all in one line
    # a.writerow(DICT.get(name,"Not Available") for name in n)

編輯

    # to write out the transpose of the previous lines (that is, instead of 
    # a line of names and a line of the matching values, a line for each
    # name-value pair), use Python's zip builtin:
    for nameValueTuple in zip(n,values):
        a.writerow(nameValueTuple)

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM