0

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.

Cause of bug

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:

CSV format before the bug

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       |

CSV Format after editing

| 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 |

Code

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
        })
Rohan Shah
  • 479
  • 3
  • 14
  • Looks like you removed the trailing newline at the end of the file. Don't do that. It may also be the Numbers app doing it. Use a different text editor in that case. Also, use `newline=''` when opening the file as indicated in the `csv` documentation. – Mark Tolonen Apr 04 '21 at 19:00
  • Thanks for the prompt response. Changing the line to `with open('/Users/Shared/PointsTracker.csv', 'a',newline='') as PointsTracker:` doesn't seem to fix the issue. Any suggestions? – Rohan Shah Apr 04 '21 at 19:03
  • `newline=''` won't fix if you removed the newline when editing the file. It's just the correct parameter to use. Put the newline back using a text editor – Mark Tolonen Apr 04 '21 at 19:04
  • Thanks a lot! This issue has been bugging me for a while now! If you want to [write the solution again as an answer](https://meta.stackoverflow.com/questions/294791/what-if-i-answer-a-question-in-a-comment) I'd be happy to mark it as correct. – Rohan Shah Apr 04 '21 at 19:09

1 Answers1

0

The trailing newline character was removed when the file was edited. Edit the file and put it back.

Mark Tolonen
  • 166,664
  • 26
  • 169
  • 251