【问题标题】:Sorting XLSX data and writing to CSV对 XLSX 数据进行排序并写入 CSV
【发布时间】:2015-08-15 03:24:16
【问题描述】:

我正在尝试获取一个大型数据文件并对其进行排序。最后我想要这样的东西:

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

我对前 20 名来电者进行排序并了解他们的来电信息。 (机器错误调用次数与用户错误调用次数)

我已经能够使用我在此博客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))

但是,整个代码很长,我无法理解它,无法提取数据并将其转换为 csv 格式。

我的问题是如何对数据进行排序并将其写入 csv?我找到了写入 csv 的方法,但很难找到关于排序顶部数据值然后制作新 csv 的任何内容。

谢谢!

【问题讨论】:

    标签: python excel sorting csv xlsx


    【解决方案1】:

    我解决了。在排序代码中,我添加了

    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]) 
    

    【讨论】:

      猜你喜欢
      • 2013-01-23
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2020-04-17
      • 2012-07-11
      • 2021-10-16
      • 2021-06-02
      • 1970-01-01
      相关资源
      最近更新 更多