How to update csv data in mutiple rows running this code mutliple times per day?

21

To append data to a file in Python, open it with "a+" mode (instead of the "w" you're currently using). This means that you'll append new data to the file every time you open and write to it.

Explanation:

  • 'a' means append.
  • '+' means it'll create the file if it doesn't exist.
Share:
21
probably-asskicker
Author by

probably-asskicker

Updated on December 20, 2022

Comments

  • probably-asskicker
    probably-asskicker over 1 year

    I am running this code multiple times per day. The data I generate is overwritten after every successful run. Here is the code:

    with open('r3edata.csv','w') as fhandle:
        writer = csv.writer(fhandle)
        items = body.items()
        writer.writerow([key for key, value in items])
        writer.writerow([value for key, value in items])
    

    This is the body dictionary:

    body = {
        'dont-ask-for-email': 0,
        'action': 'submit_user_review',
        'post_id': 76196,
        'email': email_random(),
        'subscribe': 1,
        'previous_hosting_id': prev_hosting_comp_random(),
        'fb_token': '',
        'title': review_title_random(),
        'summary': summary_random(),
        'score_pricing': star_random(),
        'score_userfriendly': star_random(),
        'score_support': star_random(),
        'score_features': star_random(),
        'hosting_type': hosting_type_random(),
        'author': x,
        'social_link': z,
        'site': '',
        'screenshot[image][]': '',
        'screenshot[description][]': '',
        'user_data_process_agreement': 1,
        'user_email_popup': '',
        'subscribe_popup': 1,
        'email_asked': 1
    }
    

    The functions I have used return different data every time.

    The output I get from saving is this:

    CSV file I get

    CSV File I want:

    CSV file I want

    The functions used in the body dict return different value every time. There will be 100 rows at the end of the day if this worked perfectly.

    TL;DR → Cannot append csv data into the final csv file I get. I have no clue how does it work.

    • Giulo Figlio
      Giulo Figlio almost 9 years
      what can I do to fix this proble?
    • Giulo Figlio
      Giulo Figlio almost 9 years
      thanks for answering, but do you mean the hard drive is full? cause I just checked and there are more than 300 Gb empty. does ubunto create a special partition to use when upgrading?
    • Rmano
      Rmano almost 9 years
      Do you have a separate /boot? Add to the question the output of df -h, please.
    • Giulo Figlio
      Giulo Figlio almost 9 years
      df -h Filesystem Size Used Avail Use% Mounted on /dev/mapper/ubuntu--vg-root 291G 16G 261G 6% / none 4,0K 0 4,0K 0% /sys/fs/cgroup udev 1,4G 4,0K 1,4G 1% /dev tmpfs 288M 1,2M 287M 1% /run none 5,0M 0 5,0M 0% /run/lock none 1,5G 272K 1,5G 1% /run/shm none 100M 48K 100M 1% /run/user /dev/sda1 236M 208M 16M 94% /boot /home/mama/.Private 291G 16G 261G 6% /home/mama
  • Giulo Figlio
    Giulo Figlio almost 9 years
    df -i Filesystem Inodes IUsed IFree IUse% Mounted on /dev/mapper/ubuntu--vg-root 19333120 360571 18972549 2% / none 368528 2 368526 1% /sys/fs/cgroup udev 364598 527 364071 1% /dev tmpfs 368528 545 367983 1% /run none 368528 4 368524 1% /run/lock none 368528 7 368521 1% /run/shm none 368528 28 368500 1% /run/user /dev/sda1 62248 325 61923 1% /boot
  • Chris Good
    Chris Good almost 9 years
    df -i shows you are not out of inodes. df -h shows problem is /boot is nearly full.
  • probably-asskicker
    probably-asskicker almost 4 years
    with open('filename.csv', 'a+', newline='') is this how I should do this?
  • Roy2012
    Roy2012 almost 4 years
    no need for the 'newline' part, as far as I understand. But other than that - yes.
  • probably-asskicker
    probably-asskicker almost 4 years
    Thanks. but how do i get rid of those repeated headings?
  • martineau
    martineau almost 4 years
    @bitch_ctrl: You should generally always use the newline='' part when reading and writing (or appending to) csv format files using the csv module. This enables it to handle embedded newline characters in column values properly — but if you never have any data like that it's easy to get wrong (and not notice as long as that's the case).
  • probably-asskicker
    probably-asskicker almost 4 years
    I used newline='' in my code. it worked @martineau