1

So, I need to review some pages, and made a janky queue for efficiency. I have a CSV that needs to be opened to be read, and one to be written to. For each page I open from the read CSV, I call input(), and write some notes, so that they can be saved to the csv to be written to. Code below.

with open("readfile.csv") as r:

  csv_file = csv.DictReader(r)

  with open("writefile.csv", 'w') as w:
    headers = {'URL': None, 'JUDGEMENT': None}
    writer = csv.DictWriter(w, fieldnames=headers)
    writer.writeheader()

    for row in csv_file:
      url = row.get("Profile URL")
      browser.get(url) //Selenium opening URL
      judgement = input("What say you?")
      writer.writerow({"Profile URL": url, "JUDGEMENT": judgement})
       

This works just fine when I do the entire CSV, but sometimes, I only want to do half. When I do CTRL+Z to escape the script, none of the write file saves. I tried adding an exception for the input like

try:
judgement = input("What say you?")
except Exception e:
//but can't find what to put here

That doesn't work, since I can't seem to find what to put here.

AcK
  • 2,063
  • 2
  • 20
  • 27
Ben Garcia
  • 376
  • 2
  • 11

1 Answers1

2

Maybe try w.close() in the exception handler - this should flush the buffer to the file, write the data, and then exit.

with open("readfile.csv") as r:

  csv_file = csv.DictReader(r)

  with open("writefile.csv", 'w') as w:
    try:
        headers = {'URL': None, 'JUDGEMENT': None}
        writer = csv.DictWriter(w, fieldnames=headers)
        writer.writeheader()

        for row in csv_file:
            url = row.get("Profile URL")
            browser.get(url) //Selenium opening URL
            judgement = input("What say you?")
            writer.writerow({"Profile URL": url, "JUDGEMENT": judgement})
    except KeyboardInterupt:
        if not w.closed:
            w.close() # Flushes buffer, and closes file

Alternatively, you could open the file for writing without a default buffer - 0 for unbuffered, 1 for line buffering (I suggest using 1):

with open("writefile.csv", 'w', buffering=1) as w

This post may help you understand further.

EDIT:

It seems as though both of these approaches are needed to solve this, opening with a line buffer, and catching the keyboard interrupt, rather than one of the two.

Tytrox
  • 483
  • 2
  • 10
  • Doesn't quite work. On the plus side, if the program exits from anything but a keyboard interrupt, then it does save my progress (which seems really weird, since the only thing I added was catching a KeyboardInterrupt exception), but alas, KeyboardInterrupt does not save. Adding the buffering=1 did make it work though. I had to use both. I did not test just the buffer. I'm going to accept this answer, but feel free to edit language since they're not alternative solutions :) – Ben Garcia May 04 '21 at 15:30