简体   繁体   English

Python:关闭一周后再写入csv,然后打开新的csv文件

[英]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. 我要做的是每隔一周写一次csv,然后关闭文件并打开一个新文件。这是基于真实的日历周。 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: 这是我正在使用的代码的一部分,在它之前有一些代码,但是它只是打开获取所需数据的iframe:

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). 我使用datetime来获取当前时间(包括日历周)。 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). 最后,我要等待60或59秒,具体取决于当前分钟的秒数(实际操作需要一些时间,否则会累积时间)。

So if you replace the write -commands by your operations, this should work. 因此,如果用您的操作替换了write -commands,这应该可以工作。

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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