Demo entry 5458602


Submitted by anonymous on Jun 26, 2016 at 07:00
Language: Python 3. Code size: 3.2 kB.

from sys import argv  # tell python we want to read the command line
import datetime		  # tell python we want to use the standard date functions 

script, inputfilename, outputfilename = argv  # read the command line into these variables

print( "Reading data from Input File: %r." % inputfilename )
print( "Writing converted data to Output File: %r." % outputfilename )

# tell python we want to read from the file specified from the commandline 
# - 'r' tells python that we want to read data from this file
inputfile = open( inputfilename, 'r')    

# tell python we want to write output results to this file, which was also 
# specified from the command line - 'w' says that we want to write or save data 
# out to this file
outputfile = open( outputfilename, 'w')  

attention = 0    # store the "attention" value from the input file into this variable
meditation = 0   # store the "meditation" value from the input file into this variable

# a "for loop" repeats blocks of code - in this case read lines from file until 
# the end of the file is reached 
# ... so here we want to loop through the input file line by line until we get to 
# the end, read each line from the file one line at at time, and store the line in 
# a variable called: line 
for line in inputfile:   

	# split the line into multiple items, looking for the ":" symbol to split on, 
	# call this 1st list variable: list1
	list1 = line.split( ':' )   

	#are there three items in the list?
	if len( list1 ) == 3:  

		#create a new list by splitting the 2nd item from the 1st list, this time split on 
		# the ' ' (space) symbol, store in variable: list2. Note python counts from 0 up
		list2 = list1[1].split(' ') 
		# the EEC data file uses a code # "04" to signify the line contains the "Attention" value
		if list2[1] == "04":		
			# save the Attention value in our 'attention' variable. 
			# The function int() convers a string of characters into a whole number
			attention = int(list1[2])		 
		# the EEC data file uses code "05" to signify the "Meditation" value
		if list2[1] == "05":	
			# save the Meditation value in our 'meditation' variable.			
			meditation = int(list1[2])		

			# The 1st item on each line of the EEC file is the timestamp recording the time that the brainwave was recorded
			# This timestamp is recorded as the number of seconds (and milliseconds) since Midnight 1/1/1970. 
		    # A standard python date/time function converts this into an variable representing the date and time
			timestamp = datetime.datetime.fromtimestamp(float(list1[0]))  

			# Now write a line showing the timestamp, the attention value, and the meditation value, to the 
			# output file, using commas to separate the file.  This 'comma separated file' can now easily be 
			# imported into Google Sheets (online spreadsheet) for reporting and graphing
			outputfile.write(str(timestamp) + "," + str(attention) + "," + str(meditation)  + "\r" )  

# now that we have finished looping through and reading all the lines from the input file, close it
# and also close the output file that we have been saving the results to

This snippet took 0.00 seconds to highlight.

Back to the Entry List or Home.

Delete this entry (admin only).