I've had a Python script running for the past 3 months. Simple scraper that grabs the date, points, and current time it collected that day and writes it into a CSV using DictWriter
.
Yesterday, I had to open the CSV (with Apple's default Numbers app) and delete the most recent entry of date, points, time. Ever since then, it has been appending the new entries into the previous row, creating extra columns. I have not edited any of the code.
Let me try to create a visual of the problem:
The top line is the headers
| Date | Points | Time Completed |
| Mar 29 | 15141 | 08:55 AM |
| Mar 30 | 15411 | 08:56 PM |
| Mar 31 | 15681 | 11:08 AM |
| Apr 01 | 15911 | 10:40 AM |
| Date | Points | Time Completed | | |
| Mar 29 | 15141 | 08:55 AM | | |
| Mar 30 | 15411 | 08:56 PM | | |
| Mar 31 | 15681 | 11:08 AM | | |
| Apr 01 | 15911 | 10:40 AMApril02 | 16276 | 07:57 PM |
I have not changed any of the code in the before/after.
def write_csv(points):
headers = ['Date', 'Points', 'Time completed']
today = date.today().strftime("%B %d")
now = datetime.now().strftime("%I:%M %p")
with open('/Users/Shared/PointsTracker.csv', 'a') as PointsTracker:
writer = csv.DictWriter(PointsTracker, fieldnames=headers)
writer.writerow({
headers[0]: today,
headers[1]: points,
headers[2]: now
})
The trailing newline character was removed when the file was edited. Edit the file and put it back.