使用Python中的DictWriter将行添加到CSV文件中

时间:2013-11-27 10:12:29

标签: python csv

我正在使用DictWriter将csv文件写入单行;只填充了两列。以下是我正在处理的一些代码片段,重点是函数 Stat2 ():

def scheduledPerformanceAvailability(FILENAME,CATEGORY):

# get category column for current entry
entry = retrieveEntries(FILENAME, CATEGORY)

# grab the most frequent entry from the collections.counter 
mostFrequent = entry.most_common(1)[0][1]   
print "\n", mostFrequent;

# calculate the total number of values in the file
totalNumber = sum(entry.values())
print "\n", totalNumber

# caculate the percentage
percentage = float( mostFrequent / totalNumber ) * 100  
print "\n", percentage, "%\n";

return percentage

def Stat2 (FILENAME2,itemValue,百分比):

    # store the values into a list
    entry = []

    percent = str(percentage)
    displayPercentage = percent + ' %'

    entry.append({'DEPARTURES_SCHEDULED': displayPercentage, 'UNIQUE_CARRIER_NAME':itemValue})

    fieldnames = ['DEPARTURES_SCHEDULED','DEPARTURES_PERFORMED','SEATS', 'UNIQUE_CARRIER_NAME']

    # open a file for writing
    outfile = open(FILENAME2, 'a')

    # create the csv writer object
    csvwriter = csv.DictWriter(outfile, delimiter=',', fieldnames=fieldnames)

    # check the file before appending any unecessary headers
    ckfile = open(FILENAME2, 'r').read()

    if(ckfile == ''):
        csvwriter.writerow(dict((fn,fn) for fn in fieldnames))

    for row in entry:
        csvwriter.writerow(row)

    # close the file
    outfile.close()

def most_commonCatgoryO_(FILENAME1,FILENAME2,CATEGORY1,CATEGORY2):

# create the field names for each category      
fieldnames = ['DEPARTURES_SCHEDULED','DEPARTURES_PERFORMED','SEATS', CATEGORY1]

# open a file for writing                                                       
outfile = open(FILENAME1,'wb')  

# create the csv writer object
csvwriter = csv.DictWriter(outfile, delimiter=',', fieldnames=fieldnames, extrasaction = 'ignore')

csvwriter.writerow(dict((fn,fn) for fn in fieldnames))

entry = retrieveEntries('input/NC_SC Proj Data_2012 {Intermediate-File}.csv', CATEGORY1)    

# grab the item value associated with the most frequent number
itemValue = entry.most_common(1)[0][0]  
# print "\n", itemValue;

# reopen the intermediate for reading
infile = open('input/NC_SC Proj Data_2012 {Intermediate-File}.csv', 'rb')
reader = csv.DictReader(infile)

# populate the outfile using the pre-defined condition
for row in reader:
    if(row[CATEGORY1] == itemValue):        
        csvwriter.writerow(row)

outfile.close()

#open the outfile for reading
with open(FILENAME1, 'rb') as infile:

    # calculate the percentage
    percentage = scheduledPerformanceAvailability(FILENAME1, CATEGORY2)
    _Stat2_(FILENAME2, itemValue, percentage)

infile.close()

例如,我的csv文件:

DEPARTURES_SCHEDULED,DEPARTURES_PERFORMED,座椅,UNIQUE_CARRIER_NAME 3.90977443609%,,,美国航空公司 4.21052631579%,,,美国航空公司 1.8045112782%,,,美国航空公司。

我想要的输出应该是:

DEPARTURES_SCHEDEDED DEPARTURES_PERFORMED SEATS UNIQUE_CARRIER_NAME

3%4%1%US Airways Inc.

0 个答案:

没有答案