在Python中使用DictWriter向CSV文件添加行
但是,我使用DictWriter将csv文件写入一行;只填充了两列。下面是我正在处理的一些代码片段,重点是函数Stat2() def scheduledPerformanceAvailability(文件名、类别): def Stat2(文件名2、项目值、百分比): 定义最常见的类别(文件名1、文件名2、类别1、类别2): 例如,我的csv文件: 预定航班、执行航班、座位、唯一承运人名称 3.90977443609%,美国航空公司。 4.21052631579%,美国航空公司。 1.8045112782%,美国航空公司 我期望的输出应该是: 离港\预定离港\表演座位唯一\承运人\姓名在Python中使用DictWriter向CSV文件添加行,python,csv,Python,Csv,但是,我使用DictWriter将csv文件写入一行;只填充了两列。下面是我正在处理的一些代码片段,重点是函数Stat2() def scheduledPerformanceAvailability(文件名、类别): def Stat2(文件名2、项目值、百分比): 定义最常见的类别(文件名1、文件名2、类别1、类别2): 例如,我的csv文件: 预定航班、执行航班、座位、唯一承运人名称 3.90977443609%,美国航空公司。 4.21052631579%,美国航空公司。 1.804511
3%4%1%US Airways Inc.当我用测试数据运行您的代码(
percent=“100”
和itemValue=10
)时,我得到:预定的航班、执行的航班、座位、唯一的承运人名称和100%,,,10
您的itemValue
或percent
值是否包含换行符?我得到的结果与beerbajay的结果类似。请更新您的问题,并向函数添加一些示例调用以及实际结果。任何生成的csv文件中都会有逗号分隔这些值。
# 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
# 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()
# 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()