Why is DictWriter not Writing all rows in my Dictreader instance?

心已入冬 提交于 2019-12-11 18:19:48

问题


I'm new to coding and by default new to Python, so please excuse my ignorance...I'm working on it.

I am trying to write some code (Python 2.7) to take specific headers from multiple CSV files and export them as a single file. Here is my code:

import csv, os

path = 'C:/Test/'

for fn in os.listdir(path):
    if ".csv" in fn:
        with open(fn, 'rb') as f:
            with open('C:/Test/fun/output.csv', 'wb') as fou:
                reader = csv.DictReader(f, delimiter=",", quotechar="|")
                writer = csv.DictWriter(fou, delimiter=",", quotechar="|", fieldnames=    ['sku', 'stock.qty', 'stock.is_in_stock'], extrasaction='ignore')
                headers = {} 
                for n in writer.fieldnames:
                    headers[n] = n
                writer.writerow(headers)
                for row in reader:
                    print row
                    writer.writerow(row)



    elif ".csv" not in fn:
        break

The print request for the reader instance seems to print all rows from multiple files. I am testing on 3 files with known rows. However, the DictWriter output file only has the rows from the last of the files read. It just doesn't make sense to me how I can print row and writerow and get different results. Obviously my DictWriter is incorrectly written but I do not see where. Probably obvious to most but I am puzzled.


回答1:


You are opening your target CSV file and clearing it for each matching CSV file you read. Opening the file in 'wb' mode clears the file each time.

Moreover, you break out of the loop as soon as you find a filename that is not a CSV file; you probably didn't want to do that at all; remove the else branch there.

Open the file just once, and continue to use it while looping over the directory, instead:

with open('C:/Test/fun/output.csv', 'wb') as fou:
    writer = csv.DictWriter(fou, delimiter=",", quotechar="|", fieldnames=    ['sku', 'stock.qty', 'stock.is_in_stock'], extrasaction='ignore')
    writer.writeheader()

    for fn in os.listdir(path):
        if ".csv" in fn:
            with open(fn, 'rb') as f:
                reader = csv.DictReader(f, delimiter=",", quotechar="|")
                for row in reader:
                    print row
                    writer.writerow(row)

I used the DictWriter.writeheader() method to write your fieldnames to the output file as an initial header.



来源:https://stackoverflow.com/questions/23153834/why-is-dictwriter-not-writing-all-rows-in-my-dictreader-instance

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!