简体   繁体   中英

Sorting XLSX data and writing to CSV

I am trying to take a large data file and sort it. In the end I want something like this:

Customer,                               Machine Error,        User Error  
Customer 1 with most calls,                #of calls,            #of calls
Customer 2 with next amount calls,         #of calls,            #of calls

Where I sort the top twenty callers and have information about their calls. (Number of machine error calls versus number of user error calls)

I've been able to sort it using code I found off of this blog https://blogs.law.harvard.edu/rprasad/2014/06/16/reading-excel-with-python-xlrd/ , which displays data this way

print ('-'*20 + '', 'Top Twenty Values', '' + '-'*20 )
print ('Value [count]')
for val, cnt in counts.most_common(20):

    print ('%s [%s]' % (val, cnt))

However, the code overall is pretty long and I'm having trouble understanding it enough to be able to pull the data and put it into csv format.

My question is how do I sort the data and write it into a csv? I found ways to write into a csv but it's difficult to find anything about sorting the top data values and then making a new csv.

Thank you!

I solved it. Inside the sorting code, I added

print ('-'*20 + '', 'Top Twenty Values', '' + '-'*20 )
print ('Value [count]') 
for val, cnt in counts.most_common(20): 
    print ('%s [%s]' % (val, cnt)) 
with open('test.csv', 'a') as f: 
    writer = csv.writer(f, delimiter=',', lineterminator='\n')
    writer.writerow([val]) 

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM