简体   繁体   English

排序XLSX数据并写入CSV

[英]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. 在这里,我对前20个呼叫者进行排序,并提供有关他们呼叫的信息。 (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 我已经能够使用从此博客https://blogs.law.harvard.edu/rprasad/2014/06/16/reading-excel-with-python-xlrd/找到的代码对它进行排序这条路

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. 但是,整个代码相当长,我很难理解它,无法提取数据并将其转换为csv格式。

My question is how do I sort the data and write it into a csv? 我的问题是如何排序数据并将其写入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. 我找到了写入csv的方法,但是很难找到有关排序顶级数据值然后创建新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]) 

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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