#1
|
|||
|
|||
Data Files
I'm creating a data file for the program I wrote. Here is the setup. I have an input named subject called by viz.input. Then I say the following in the data file:
Code:
start_time = time.time() question_data = open('study_data','a') elapsed_time = time.time() -start_time data = 'Subject is ' + str(subject) question_data.write(data) question_data.flush() Subject is Subject is 1 |
#2
|
|||
|
|||
Hi,
You're opening the file for appending. So it is adding the data to the existing data in the file. If you want to create the file from scratch open it using the 'w' flag: Code:
question_data = open('study_data','w') |
#3
|
|||
|
|||
Yes, I understand I can use the 'w' flag but what happens when I want to add data to the same data file. I can use the 'w' flag for the first time, but then i'd want to append to the file after that. I tried the 'w' flag and it printed out correctly, but then when I changed it to the 'a' flag, it went back to printing strangely again.
The 'w' flag prints like this: Subject 12 Condition 1 The 'a' flag prints like this: Subject Condition Subject 12 Condition 2 |
#4
|
|||
|
|||
Hi,
Then I'm not sure exactly what you want. You can either append the data to the file using the 'a' flag, or you can override the existing data using the 'w' flag. What other option do you need? |
#5
|
|||
|
|||
No problem,
I figured out the problem. I had to put those statements in a onkeydown event. Thanks! |
#6
|
|||
|
|||
I created this data file but one question I had was whether there was a way to place Headings at the beginning of the file. If you notice right now, there is text (eg 'male count', 'female count', etc) before the actual data used to identify the data that is being shown during a question_data.write() command. Is there a way to only put those headings at the beginning of the data file instead of having them included in the write command. I hope I'm being clear. Here is an example I typed out (attachment)
Code:
#Mark the time that the program was started. start_time_sec = time.time() start_time_clock = time.ctime() #Open a file to collect the data on this subject. question_data = open('omar_data','a') #Get the data from the button press. def mykey(key): global autospawn, soldierCount, maleCount, femaleCount if key == 'a': autospawn = not autospawn if autospawn: viz.starttimer(SPAWN_TIMER,GetSpawnTime()) else: viz.killtimer(SPAWN_TIMER) elif key == 's': StartAvatar(randint(0,5)) elif key == 'q': miss_count = bulletCount - avatarsHit #print "miss count is", miss_count #Use our starting time variable to find out how much #time has elapsed. elapsed_time_sec = time.time() - start_time_sec #elapsed_time_clock = time.ctime(elapsed_time_sec) + time.ctime(start_time_sec) final_time_sec = start_time_sec + elapsed_time_sec final_time_clock = time.ctime(final_time_sec) #Create string lines to put in the data file, depending on which button was pushed. subject_data = str(subject) + '\t' start_data = str(start_time_clock) + '\t' final_time_data = str(final_time_clock) + '\t' avatar_hit_count = str(avatarsHit) + '\t' bullet_count_data = str(bulletCount) + '\t' miss_count_data = 'miss count ' + str(miss_count) + '\t' soldier_Count_data = 'soldier count ' + str(soldierCount) + '\t' male_Count_data = 'male count ' + str(maleCount) + '\t' female_Count_data = 'female count ' + str(femaleCount) + '\t' threat_Count_data = 'threat count ' + str(threatCount) + '\t' nonthreat_Count_data = 'nonthreat count ' + str(nonthreatCount) + '\t\n' question_data.write(subject_data) question_data.write(start_data) question_data.write(final_time_data) question_data.write(avatar_hit_count) question_data.write(bullet_count_data) question_data.write(miss_count_data) question_data.write(soldier_Count_data) question_data.write(male_Count_data) question_data.write(female_Count_data) question_data.write(threat_Count_data) question_data.write(nonthreat_Count_data) #Flush the internal buffer. question_data.flush() #Close the world. viz.quit() viz.callback(viz.KEYBOARD_EVENT,mykey) |
#7
|
|||
|
|||
Hi,
After you open the file, check if it is empty. If it is then write the headings. Code:
data = open('test.data','a') #Seek to end of file data.seek(0,2) if data.tell() == 0: #File is empty, write heading data.write('Column 1\tColumn 2\tColumn 3\n') data.write('some data') |
|
|