簡體   English   中英

Python 3.4 使用 time.sleep() 寫入文件的問題

[英]Python 3.4 Issue with writing to a file with time.sleep()

我正在使用 Python 3.4。 我試圖弄清楚為什么如果我在最后放置了一個睡眠計時器,它不會將數據寫入 Test.dat 文件,那么為什么 writerow() 不起作用。 代碼看起來像這樣

import csv, time

fileName = "Test.dat"
freq = 5; 

with open(fileName, 'a') as fp:
    a = csv.writer(fp, delimiter=',')
    for i in range(1, 10):
        data = [2014, 5, 3, 5, 26, 53, 'Price', '100.00']
        a.writerow(data)
        time.sleep(freq)

但是,如果我刪除計時器,它會將數據寫入 Test.dat 文件

import csv, time

fileName = "Test.dat"
freq = 5; 

with open(fileName, 'a') as fp:
    a = csv.writer(fp, delimiter=',')
    for i in range(1, 10):
        data = [2014, 5, 3, 5, 26, 53, 'Price', '100.00']
        a.writerow(data)

答:

2014,5,3,5,26,53,Price,100.00
2014,5,3,5,26,53,Price,100.00
2014,5,3,5,26,53,Price,100.00
2014,5,3,5,26,53,Price,100.00
2014,5,3,5,26,53,Price,100.00
2014,5,3,5,26,53,Price,100.00
2014,5,3,5,26,53,Price,100.00
2014,5,3,5,26,53,Price,100.00
2014,5,3,5,26,53,Price,100.00

您正在等待 5 秒,程序需要很長時間才能結束寫入文件:

等一秒鍾:

freq = 1

您可以拾取一些緩沖液沖洗材料。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM