国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 編程 > Python > 正文

python實現(xiàn)一組典型數(shù)據(jù)格式轉(zhuǎn)換

2020-01-04 13:48:39
字體:
供稿:網(wǎng)友

本文實例為大家分享了一組典型數(shù)據(jù)格式轉(zhuǎn)換的python實現(xiàn)代碼,供大家參考,具體內(nèi)容如下

有一組源數(shù)據(jù),第一行會是個日期數(shù)據(jù),第二行標(biāo)明字段,再接下來是兩行數(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

這個時候可以使用Python來處理,代碼如下:

# coding = utf-8fd = 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()

生成文件格式如下,基本上滿足了需求。

python,數(shù)據(jù)格式轉(zhuǎn)換

以上就是本文的全部內(nèi)容,希望對大家的學(xué)習(xí)有所幫助,也希望大家多多支持VEVB武林網(wǎng)。


注:相關(guān)教程知識閱讀請移步到python教程頻道。
發(fā)表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發(fā)表
主站蜘蛛池模板: 红河县| 会泽县| 德州市| 响水县| 德江县| 察哈| 奎屯市| 长宁区| 山丹县| 保山市| 松阳县| 南江县| 诏安县| 民丰县| 晋江市| 奉节县| 蓬莱市| 北海市| 黄冈市| 花莲市| 霍州市| 塔河县| 茌平县| 龙井市| 南阳市| 上高县| 宁海县| 新宾| 修武县| 海盐县| 都江堰市| 鹤山市| 广饶县| 遂宁市| 改则县| 巴东县| 吉首市| 阳新县| 镇平县| 岳西县| 昔阳县|