I am writing one python script. Need your help in correcting my script in python 3.x or python 2.6
For each input file need sperate output file in another directory
we have multiple input file with name suppose(1.1.1.2.txt, 1.1.1.3.txt) with kind of same set of data separated by blank line
Need to grep below line from input file(which is text file)
and keep "ave rate from user(bps)" as column heading and "17104773" as its value in row and similarly "ave rate to user(bps)" in another column with heading and "247272821" its value in row in another CSV FILE.
session: 43 ave rate from user(bps) : 17104773 ave rate to user(bps) : 247272821
Input file : after running command
show sub data-rate smgr-instance 43
need to grep and put 43 in output file first column. This 43 is dynamic value
[local]AM-SI-SP-01# show sub data-rate smgr-instance 43
Thursday July 1 17:10:45 IST
Total Subscribers : 2978
Active : 2978 Dormant : 0
peak rate from user(bps): n/a* peak rate to user(bps) : n/a*
ave rate from user(bps) : 17104773 ave rate to user(bps) : 247272821
sust rate from user(bps): 16999794 sust rate to user(bps) : 246777077
peak rate from user(pps): n/a* peak rate to user(pps) : n/a*
ave rate from user(pps) : 13117 ave rate to user(pps) : 27879
sust rate from user(pps): 13233 sust rate to user(pps) : 27976
[local]AM-SI-SP-01# show sub data-rate smgr-instance 45
Thursday July 1 17:10:45 IST
Total Subscribers : 2978
Active : 2978 Dormant : 0
peak rate from user(bps): n/a* peak rate to user(bps) : n/a*
ave rate from user(bps) : 17104234 ave rate to user(bps) : 24728978
sust rate from user(bps): 16999794 sust rate to user(bps) : 246777077
peak rate from user(pps): n/a* peak rate to user(pps) : n/a*
ave rate from user(pps) : 13117 ave rate to user(pps) : 27879
sust rate from user(pps): 13233 sust rate to user(pps) : 27976
My code :
import os
import csv
import re
from io import BytesIO
def parseFile(somefile):
data = dict()
with open(somefile, 'r') as f :
lines=f.read()
sub="ave rate from user(bps)"
if (lines.find(sub))==-1:
print ("nothing")
else:
splitted = lines.split(':')
data["Hostname"] = splitted[1]
print (data)
#print("mylines",lines)
for line in lines:
line = line.rstrip("\n")
print ("line",line)
#print (lines)
#print (type(line))
if (lines.startswith(sub)):
print ("nothing")
#continue;
else:
print (data)
#return data
#print (data)
parseFile("C:\\Users\\ajachaud\Desktop\\scripts\\abc\\fileName.txt")
if __name__ == "__main__":
inputsDirectory = "C:/Users/ajachaud/Desktop/scripts"
path = os.path.abspath(inputsDirectory)
fileList = ["{}/{}".format(path,x) for x in os.listdir(inputsDirectory)]
# print(fileList)
# Load Each File and Build Dictionary
csvRows = []
for file in fileList:
newRow = parseFile(file)
csvRows.append(newRow)
# print(csvRows)
# Output CSV using dictionaries for each file
outputFile = "output.csv"
with open(outputFile, 'w', newline='') as csvfile:
fieldnames = ["Hostname",
"loopback"]
writer = csv.DictWriter(csvfile, fieldnames=fieldnames)
writer.writeheader()
for row in csvRows:
writer.writerow(row)
Expected output in csv file :
session,ave rate from user(bps),ave rate to user(bps)
43 17104773 247272821
45 17104234 24728978