简体   繁体   中英

Python: Write to csv for a week close and open new csv file

Looking for a point in the right direction or help. i know it is something i am doing wrong, but i am new and still trying to learn so guidance is always wanted and needed.

I have looked in the question sections and can not find something that works. I have tried a bunch of solutions but they have not worked, unless i am doing them wrong. What i am trying to do is write to a csv once every minuet for a week then close the file and open a new one.This is based on true calendar weeks. I am also having trouble with the loop. I can open and write to a file the way i want but not get it to write for the period of time that i want.

Here is part of the code i am working with, there is some code before it but it is just opening the iframes getting the data i need:

file_name = 'Tempdata.csv'

headers = (['Space', 'South Wall Int.','','','Space', 'South Wall Ext.')
headers2 =(['Timestamp','Time','Date','Temperature','Timestamp',
'Time','Date','Temperature'])

if os.path.isfile(file_name):
    with open('Tempdata.csv', 'a', newline='') as f:
        w = csv.writer(f)
        line=()
        for frag in data:
            line+=frag
        w.writerow(line)
else:
    with open(file_name, 'w', newline='') as f:
        w = csv.writer(f)
        w.writerow(headers)
        w.writerow(headers2)
        line=()
        for frag in data:
        line+=frag
        w.writerow(line)

nos=25  # Number of samples
req=5  # Sampling frequency
print("\nTemperature & timestamp, sampled every", freq, "seconds:")

while True:
    for i in range(nos):
        ts=driver.find_element_by_name("_lastUpdated").get_attribute
        ("value")
        #print("\nTemperature: ", element.text, "read at:", ts)
        time.sleep(freq)

driver.switch_to_default_content()

print ("Done.")

Since your code is not exactly minimal, I did not read much of it, but went by your verbal description.

I used datetime to get the current time (including calendar week). When there is a new week, I close the old one and open a new file. In the end I wait 60 or 59 seconds, depending on how many second of the current minute have passed (the actual operations take some time, which otherwise accumulates).

So if you replace the write -commands by your operations, this should work.

import datetime
import time

old_week = -1 
writer = None

while True:
  dt = datetime.datetime.now()
  calendar_week = dt.isocalendar()[1]
  minute = dt.minute
  if calendar_week > old_week:
    old_week = calendar_week
    if writer is not None:
      writer.close()
    #replace accordingly
    writer = open('log_%s.csv' % calendar_week, 'w')
    writer.write('week; minute; actual\n')
  #replace accordingly
  writer.write('%s;%s;%s\n' % (calendar_week, minute, dt.isoformat()))
  if dt.second < 30:
    time.sleep(60)
  else:
    time.sleep(59)
writer.close()

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM