Project

File Parsing in Python

OBJECTIVE: To demonstate file parsing using Python.

  1. when we run the program, first thing appears is to enter the filename,if we speicify correct name then we can see the rest output otherwise a message will show file not found.
  2. To store different properties like crank, pressure, density,etc empty array was created.
  3. Using for loop each line was read and each line was split using split() by empty space.
  4. As crank was index 0, so we use crank.append(float(line.split()[0])). Same procedure was repeated for all other properties.
  5. To make a column of properties like density, mass, crank, etc again for loop was used and since these properties are present on line 3, if command was used if linecount is 3:
    title.append(line.split()) Here also empty space was used for splitting and all data was stored in array title.
  6. Same this was done to extract units from line no 4 and was stored in array units.
  7. To construct a table PrettyTable was imported and table consisting of column and properties was created. since Title array conatined list inside a list [0] was used to extract 1st list and [x+1] was used since first value(#) was not needed.
  8. ALL values of engine data was stored in in variable a column wise like 1st column contained only crank data.
  9. To plot the data between two values like pressure and volume, input x and y was taken and these input aws converted into integers.
  10. plotting was done using a[x] and a[y].
  11. For label data was taken from title and units.
  12. Area was calculated using np.trapz(pressure, volume)
  13. power aws calculated using formula (area underpv plot/time for one cycle)
  14. brake specific fuel consumption was calculated using formula (3.6*10^6)*(m/areaunder pv plot).
#Demonstrating file parsing in python
#Luv Kumar
import matplotlib.pyplot as plt
import math
import numpy as np
from prettytable import PrettyTable

filename = input(\'enter file name\')
if filename.endswith(\'.out\'):
	
	crank = []
	pressure = []
	max_pressure = []
	min_pressure = []
	mean_temp =[]
	max_temp = []
	min_temp= []
	volume = []
	mass= []
	density = []
	integrated_HR = []
	HR_rate = []
	C_p = []
	C_v = []
	gamma = []
	kin_visc = []
	Dyn_visc = []



	for line in  open(\'engine_data.out\'):
		if \'#\' not in line:
			crank.append(float(line.split()[0]))
			pressure.append(float(line.split()[1]))
			max_pressure.append(float(line.split()[2]))
			min_pressure.append(float(line.split()[3]))
			mean_temp.append(float(line.split()[4]))
			max_temp.append(float(line.split()[5]))
			min_temp.append(float(line.split()[6]))
			volume.append(float(line.split()[7]))
			mass.append(float(line.split()[8]))
			density.append(float(line.split()[9]))
			integrated_HR.append(float(line.split()[10]))
			HR_rate.append(float(line.split()[11]))
			C_p.append(float(line.split()[12]))
			C_v.append(float(line.split()[13]))
			gamma.append(float(line.split()[14]))
			kin_visc.append(float(line.split()[15]))
			Dyn_visc.append(float(line.split()[16]))


	#taking units out of data

	units = []
	title = []
	#taking title from data

	linecount = 1
	for line in open(\'engine_data.out\'):
		if linecount is 3:
			title.append(line.split())
		if linecount is 4:
			units.append(line.split())
		linecount=linecount +1

	t=[1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17]


	print(\'All properties in given data\')
	table = PrettyTable([\'column\',\'properties\'])
	for i in range(0,len(t)):
		table.add_row([t[i],title[0][i+1]])
	

	print(table)




	a =[crank,pressure,max_pressure,min_pressure,mean_temp,max_temp,min_temp,volume,mass,density,integrated_HR,HR_rate,C_p,C_v,gamma,kin_visc,Dyn_visc]
	x = int(input(\"give the first column number.\"))
	y = int(input(\"give the second column number.\"))
	plt.plot(a[x],a[y],linewidth=3)
	plt.xlabel(title[0][x+1]+units[0][x+1])
	plt.ylabel(title[0][y+1]+units[0][y+1])
	plt.show()




	pressure = np.array(pressure)*1e6
	volume = np.array(volume)
	pvarea = np.trapz(pressure,volume)
	print(pvarea,\"joules\")

	#power output
	rpm = 1500
	timefor1rev = 60/1500 # time for one revolution in sec

	#one rev = 2strokes
	#one cycle = 4strokes
	#one cycle= 2revolution
	

	





	time1cycle= 2*timefor1rev
	power = pvarea/time1cycle
	print(power,\'watt\')
	print(power*0.00135962,\'HP\')



	#calculating specific fuel consumption (brakespecificfuelconsumption)

	m = 20*1e-6
	bsfc= 3.6*1e6*(m/pvarea)
	print(bsfc,\'g/kw.H\')

else:

	 print(\'file not found\')
​

When typed column no 8 and column no 2, we get above plot.

#Demonstrating file parsing in python
#Luv Kumar
import matplotlib.pyplot as plt
import math
import numpy as np
from prettytable import PrettyTable

filename = input(\'enter file name\\n\')
if filename.endswith(\'.out\'):
	
	crank = []
	pressure = []
	max_pressure = []
	min_pressure = []
	mean_temp =[]
	max_temp = []
	min_temp= []
	volume = []
	mass= []
	density = []
	integrated_HR = []
	HR_rate = []
	C_p = []
	C_v = []
	gamma = []
	kin_visc = []
	Dyn_visc = []



	for line in  open(\'engine_data.out\'):
		if \'#\' not in line:
			crank.append(float(line.split()[0]))
			pressure.append(float(line.split()[1]))
			max_pressure.append(float(line.split()[2]))
			min_pressure.append(float(line.split()[3]))
			mean_temp.append(float(line.split()[4]))
			max_temp.append(float(line.split()[5]))
			min_temp.append(float(line.split()[6]))
			volume.append(float(line.split()[7]))
			mass.append(float(line.split()[8]))
			density.append(float(line.split()[9]))
			integrated_HR.append(float(line.split()[10]))
			HR_rate.append(float(line.split()[11]))
			C_p.append(float(line.split()[12]))
			C_v.append(float(line.split()[13]))
			gamma.append(float(line.split()[14]))
			kin_visc.append(float(line.split()[15]))
			Dyn_visc.append(float(line.split()[16]))


	#taking units out of data

	units = []
	title = []
	#taking title from data

	linecount = 1
	for line in open(\'engine_data.out\'):
		if linecount is 3:
			title.append(line.split())
		if linecount is 4:
			units.append(line.split())
		linecount=linecount +1

	t=[1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17]


	print(\'All properties from given data\')
	table = PrettyTable([\'column\',\'properties\'])
	for i in range(0,len(t)):
		table.add_row([t[i],title[0][i+1]])
	

	print(table)




	a =[crank,pressure,max_pressure,min_pressure,mean_temp,max_temp,min_temp,volume,mass,density,integrated_HR,HR_rate,C_p,C_v,gamma,kin_visc,Dyn_visc]
	x = int(input(\"give the first column number.\"))
	y = int(input(\"give the second column number.\"))
	plt.plot(a[x-1],a[y-1],linewidth=3)
	plt.xlabel(title[0][x]+units[0][x])
	plt.ylabel(title[0][y]+units[0][y])
	plt.show()




	pressure = np.array(pressure)*1e6
	volume = np.array(volume)
	pvarea = np.trapz(pressure,volume)
	print(pvarea,\"joules\")

	#power output
	rpm = 1500
	timefor1rev = 60/1500 # time for one revolution in sec

	#one rev = 2strokes
	#one cycle = 4strokes
	#one cycle= 2revolution
	t=[1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17]


	





	time1cycle= 2*timefor1rev
	power = pvarea/time1cycle
	print(power,\'watt\')
	print(power*0.00135962,\'HP\')



	#calculating specific fuel consumption (brakespecificfuelconsumption)

	m = 20*1e-6
	bsfc= 3.6*1e6*(m/pvarea)
	print(bsfc,\'g/kw.H\')

else:
	 print(\'file not find\')

Your portfolio, next

Ship work like this
in your own course.

Talk to a career counsellor

Free 20-minute call. We'll map the right course to where you want to work.

By submitting you agree to our Terms and Privacy Policy.

Talk to a career counsellor

Free 20-minute call. We'll map the right course to where you want to work.

By submitting you agree to our Terms and Privacy Policy.