python实现一组典型数据格式转换
作者:杰瑞26 时间:2023-07-14 22:19:04
本文实例为大家分享了一组典型数据格式转换的python实现代码,供大家参考,具体内容如下
有一组源数据,第一行会是个日期数据,第二行标明字段,再接下来是两行数据行。
1018 14:31:30.193
Type Succ Fail
sour_sm 1308 1205
data_sm 2205 3301
1019 16:32:30.201
Type Succ Fail
data_sm 3308 2206
data_sm 1765 1105
1020 18:00:00.203
Type Succ Fail
sour_sm 7804 1105
data_sm 2976 1300
要转换成数据
Time Type Succ Fail Total
1018 14:31:30.193 sour_sm 1308 1205 2513
1018 14:31:30.193 data_sm 2205 3301 5506
1019 16:32:30.201 data_sm 3308 2206 5514
1019 16:32:30.201 data_sm 1765 1105 2870
1020 18:00:00.203 sour_sm 7804 1105 8909
1020 18:00:00.203 data_sm 2976 1300 4276
这个时候可以使用Python来处理,代码如下:
# coding = utf-8
fd = open(r"output.txt", "w", encoding="utf-8")
fd.write("%s\t\t\t\t%s\t%s\t%s\t%s\n" % ("Time", "Type", "Succ", "Fail", "Total"))
with open(r"data.txt", "r", encoding="utf-8") as fd1:
lines = fd1.readlines()
time1 = lines[0::4]
data1 = lines[2::4]
data2 = lines[3::4]
for (i, line) in enumerate(time1):
Time = line.strip()
Type_1 = data1[i].strip().split()[0]
Succ_1 = data1[i].strip().split()[1]
Fail_1 = data1[i].strip().split()[2]
Total_1 = str(int(Succ_1) + int(Fail_1))
Type_2 = data2[i].strip().split()[0]
Succ_2 = data2[i].strip().split()[1]
Fail_2 = data2[i].strip().split()[2]
Total_2 = str(int(Succ_2) + int(Fail_2))
fd.write("%s\t%s\t%s\t%s\t%s\n" % (Time, Type_1, Succ_1, Fail_1, Total_1))
fd.write("%s\t%s\t%s\t%s\t%s\n" % (Time, Type_2, Succ_2, Fail_2, Total_2))
fd.close()
生成文件格式如下,基本上满足了需求。
来源:https://blog.csdn.net/Jerry_1126/article/details/84930210
标签:python,数据,格式转换
0
投稿
猜你喜欢
python如何操作mysql
2024-01-16 23:27:28
PHP对象克隆clone用法示例
2024-05-02 17:33:51
大数据量时提高分页的效率
2024-01-25 09:07:37
asp如何制作一个简单的翻页程序?
2010-06-29 21:26:00
MySQL数据库中应当如何实施info()函数
2008-11-27 15:04:00
tensorflow 中对数组元素的操作方法
2022-01-22 11:35:37
远程过程调用RPC基本概念及实现原理
2022-09-24 06:45:30
Python+selenium破解拼图验证码的脚本
2023-11-22 05:24:05
Python实现一键整理百度云盘中重复无用文件
2023-09-23 04:33:38
python使用Pandas库提升项目的运行速度过程详解
2021-07-21 12:42:29
Oracle SQL性能优化系列学习二
2010-07-23 13:23:00
Python Opencv图像处理基本操作代码详解
2023-06-08 08:08:05
基于python图像处理API的使用示例
2022-12-19 02:14:53
oracle数据库sql的优化总结
2024-01-23 16:05:11
详谈python中subprocess shell=False与shell=True的区别
2021-08-01 02:00:01
实例详解Python中的numpy.abs和abs函数
2023-03-09 08:06:38
用Frontpage设计网站主页
2008-10-23 13:44:00
sql server 编译与重编译详解
2024-01-14 11:02:59
Go语言hello world实例
2024-04-26 17:21:32
Python画图时如何调用本地字体
2023-08-02 08:51:31