0

I have a python script that tell me what 'period' it is (for school) which then outputs into a csv file:

import datetime
import csv

format = "%H%M"
today = datetime.datetime.today()
s = today.strftime(format)
period = 0

ofile = open('Period.csv', "wb")
writer = csv.writer(ofile, delimiter=',')

if s > "0845" < "0945":
 period = 1
if s >= "0945" < "1120":
 period = 2
if s >= "1120" < "1220":
 period = 3
if s >= "1220" < "1335":
 period = 4 
if s >= "1335" < "1430":
 period = 5

print
print s
print period

writer.writerow([period])
ofile.close() 

I have another python script that grabs a student's name once they swipe their student card through a magnetic card reader and then outputs that into a CSV file.

What I want to happen is when they swipe their card their name gets imported into the CSV then the period gets imported next to it (ex. john,4). Then the next student will swipe their card and their name and period will be placed under the last students

 John,4
 Marcus,4

But every time a card it swiped the csv file gets erased and then the current students name is the only one left in there.

 import csv
 import datetime

 ## EQ ID

 eqid = "john" ## Output from magnetic card reader


 ## CSV writer

 ofile = open('attendance.csv', "wb")
 writer = csv.writer(ofile, delimiter=',')
 writer.writerow([eqid])
 ofile.close() 

This is just a temp mock up the final code will have both codes in it.

TL;DR - how do I have python add data to a CSV file instead of just rewriting the whole file?

Guillaume Jacquenot
  • 11,217
  • 6
  • 43
  • 49
Sirsyorrz
  • 45
  • 6
  • Similar question here: https://stackoverflow.com/questions/4706499/how-do-you-append-to-a-file – Fosa Jul 20 '17 at 03:01
  • 3
    open the csv in `a+` mode – gold_cy Jul 20 '17 at 03:04
  • *Aside*: `if s > "0845" < "0945":` doesn't do what you think it does. Perhaps you meant `if "0845" < s < "0945":`. – Robᵩ Jul 20 '17 at 03:07
  • @Robᵩ It seems to work when I have been testing it. what is the difference between having 's' at the start or 's' in the middle?? – Sirsyorrz Jul 20 '17 at 03:14
  • Did you read the docs for [open](https://docs.python.org/3/library/functions.html#open)? You should open fit appending. – wwii Jul 20 '17 at 03:19
  • @Sirsyorrz: `s > "0845" < "0945"` firsts compares `s` with `"0845"`, then compares `"0845"` with `"0945"`. The latter comparison will always be trrue. Test it yourself: `s = "1100"; print(s > "0845" < "0945")` – Robᵩ Jul 20 '17 at 03:29
  • @Robᵩ ah yes, thank you. I will have to use "0845" > s < "0945": though as I want s to be greater then the start time and less then the finish time. – Sirsyorrz Jul 24 '17 at 00:11

1 Answers1

0

But every time a card it swiped the csv file gets erased and then the current students name is the only one left in there.

This is because you are writing ("wb") the file every time the card is swiped, which means a new file is created (an existing file with the same name will be erased). So You should use "ab" mode which is appending values in existing file. So the line

ofile = open('attendance.csv', "wb")

should be changed to

ofile = open('attendance.csv', "ab")

Now everytime card is swiped the values will be appended.

you can check File Operations at https://docs.python.org/3/tutorial/inputoutput.html#reading-and-writing-files

Bijoy
  • 1,131
  • 1
  • 12
  • 23