열과 데이터를 추출하려고하는 텍스트 파일이 있습니다. 데이터의 샘플은 다음과 같은 :파이썬을 사용하여 텍스트 파일의 열에 필드 추가
나는 그런 다음 새로운으로, 이들의 각 반복에 대한 값을 추가 ... 등등 총-TX, 드롭 속도 등의 항목에서 열을 만들 취할 필요-Global stats enabled
Cpu Utilization : 0.1 % 45.4 Gb/core
Platform_factor : 1.0
Total-Tx : 270.75 Mbps
Total-Rx : 0.00 bps
Total-PPS : 37.98 Kpps
Total-CPS : 0.00 cps
Expected-PPS : 102.71 Kpps
Expected-CPS : 2.78 Kcps
Expected-BPS : 764.51 Mbps
Active-flows : 366 Clients : 252 Socket-util : 0.0023 %
Open-flows : 2792 Servers : 65534 Socket : 366 Socket/Clients : 1.5
drop-rate : 270.75 Mbps
current time : 7.6 sec
test duration : 3592.4 sec
-Latency stats enabled
Cpu Utilization : 0.0 %
if| tx_ok , rx_ok , rx check ,error, latency (usec) , Jitter max window
| , , , , average , max , (usec)
----------------------------------------------------------------------------------------------------------------
0 | 1116, 0, 0, 0, 0 , 0, 0 | 0 0 0 0 0 0 0 0 0 0 0 0 0
1 | 1116, 0, 0, 0, 0 , 0, 0 | 0 0 0 0 0 0 0 0 0 0 0 0 0
2 | 1116, 0, 0, 0, 0 , 0, 0 | 0 0 0 0 0 0 0 0 0 0 0 0 0
3 | 1116, 0, 0, 0, 0 , 0, 0 | 0 0 0 0 0 0 0 0 0 0 0 0 0
[2J[2H
-Per port stats table
ports | 0 | 1 | 2 | 3
-----------------------------------------------------------------------------------------
opackets | 30391 | 48748 | 30360 | 48696
obytes | 2468147 | 68386300 | 2465677 | 68310324
ipackets | 0 | 0 | 0 | 0
ibytes | 0 | 0 | 0 | 0
ierrors | 0 | 0 | 0 | 0
oerrors | 0 | 0 | 0 | 0
Tx Bw | 4.77 Mbps | 130.69 Mbps | 4.76 Mbps | 130.53 Mbps
열.
현재 나는 그러나 csv 파일에 관련 데이터가있는 행을 추가 도움이 필요, 열을 추출 할 수 있습니다 :import csv
import itertools
with open('output.txt', 'r') as in_file:
stripped = (line.strip() for line in in_file)
lines = (line for line in stripped if line)
grouped = itertools.izip(*[lines] * 4)
with open('output_stats.csv', 'w') as out_file:
writer = csv.writer(out_file)
writer.writerow(('current time', 'drop-rate', 'Total-Tx', 'Total-Rx'))
writer.writerows(grouped)
예,이 시점에서 나는':' 을 포함하는 행에 대한 항목 만 저장하면되므로'split (:)'을 사용해 볼 수 있습니다. –