python實(shí)現(xiàn)一組典型數(shù)據(jù)格式轉(zhuǎn)換
本文實(shí)例為大家分享了一組典型數(shù)據(jù)格式轉(zhuǎn)換的python實(shí)現(xiàn)代碼,供大家參考,具體內(nèi)容如下
有一組源數(shù)據(jù),第一行會(huì)是個(gè)日期數(shù)據(jù),第二行標(biāo)明字段,再接下來(lái)是兩行數(shù)據(jù)行。
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
要轉(zhuǎn)換成數(shù)據(jù)
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
這個(gè)時(shí)候可以使用Python來(lái)處理,代碼如下:
# 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()
生成文件格式如下,基本上滿足了需求。

以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
Django自帶用戶認(rèn)證系統(tǒng)使用方法解析
這篇文章主要介紹了Django自帶用戶認(rèn)證系統(tǒng)使用方法解析,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下2020-11-11
分享3個(gè)簡(jiǎn)單的Python代碼高效運(yùn)行技巧
這篇文章主要介紹了分享3個(gè)簡(jiǎn)單的Python代碼高效運(yùn)行技巧,下面主要分享三個(gè)有效的,方便理解的,執(zhí)行高效的實(shí)用技巧,需要的朋友可以參考一下2022-03-03
淺談對(duì)pytroch中torch.autograd.backward的思考
這篇文章主要介紹了對(duì)pytroch中torch.autograd.backward的思考,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2019-12-12
WINDOWS 同時(shí)安裝 python2 python3 后 pip 錯(cuò)誤的解決方法
這篇文章主要給大家分享的是在WINDOWS下同時(shí)安裝 python2 python3 后 pip 錯(cuò)誤的解決方法,非常的實(shí)用,有需要的小伙伴可以參考下2017-03-03
Python使用django框架實(shí)現(xiàn)多人在線匿名聊天的小程序
很多網(wǎng)站都提供了在線匿名聊天的小功能,下面小編基于python的django框架實(shí)現(xiàn)一個(gè)多人在線匿名聊天的小程序,具體實(shí)現(xiàn)代碼大家參考下本文2017-11-11
python處理 yaml 時(shí)保持輸入輸出格式一致的問(wèn)題記錄
這篇文章主要介紹了python處理 yaml 時(shí)保持輸入輸出格式一致的問(wèn)題記錄,要想保持順序不變?cè)赿ump時(shí)添加sort_keys=False,使yaml格式保持原來(lái)的排序,本文給大家介紹的非常詳細(xì),需要的朋友可以參考下2024-06-06

