使用Python提取txt文件中的数据到excel中

详情

最近在做服务组合的一些研究,从作者那获取到了QWS数据集,因此需要对数据集进行处理,获取到数据集的数据,保存在excel文件中。QWS数据集包含2000多条数据,每条数据之间的数值采用“,”隔开,因此可以使用spilt函数进行分片处理。

QWS数据集文件

《使用Python提取txt文件中的数据到excel中》

数据处理的步骤

  1. 切换到要处理的文件的路径
os.chdir('G:/')
  1. 创建excel对象以及sheet对象,excel是数据处理后存放的位置,sheet是excel文件的一个表格,文件名为result_QoS,表格名为output。
# 创建一个excel文件:result_QoS
result_QoS = xlwt.Workbook(encoding='utf-8', style_compression=0)

# 创建sheet对象,一个sheet对象对应execl文件中的一个sheet表格
sheet = result_QoS.add_sheet('output', cell_overwrite_ok=True)
  1. 向表格中添加表题【我需要获得相邻”,”之间的数据,每一条数据共包含11个数据项】
sheet.write(0, 0, 'response_time')   # 执行时间
sheet.write(0, 1, 'availability')    # 可用性
sheet.write(0, 2, 'throughput')      # 吞吐量
sheet.write(0, 3, 'successability')  # 执行成功率
sheet.write(0, 4, 'reliability')     # 可靠性
sheet.write(0, 5, 'compliance')
sheet.write(0, 6, 'best_practices')
sheet.write(0, 7, 'latency')
sheet.write(0, 8, 'documentation')
sheet.write(0, 9, 'service_name')
sheet.write(0, 10, 'WSDL_address')
  1. 对要处理的“QWS数据集1.txt”文件进行切片处理,获取到需要的数据,并存储到excel文件中
# 对txt根据','进行切片
n = 1
with open('QWS数据集1.txt','r+') as f:
    for line in f.readlines():
        for i in range(11):
            sheet.write(n, i, line.split(',')[i])
        n = n + 1

  1. 将输出数据保存在指定的excel文件中【我保存在相同目录下的”QWS数据集.xls”文件】
result_QoS.save('QWS数据集1.xls')

数据处理后的结果

《使用Python提取txt文件中的数据到excel中》

    原文作者:Java菜鸟进阶中
    原文地址: https://blog.csdn.net/m0_46343314/article/details/120942530
    本文转自网络文章,转载此文章仅为分享知识,如有侵权,请联系博主进行删除。
点赞