简体   繁体   中英

writing into .csv-file inside of “while”-loop

By using

result = open("data/"+ name + "_" + timestamp + ".csv", "w")
result.write("time; data1; data2; data3 \n")`

I open a file and fill it with the column identifiers.

Using

while True:
    timestamp = time.strftime("%H:%M:%S", time.localtime())
    data1,data2,data3 = device.fetchData()

    result.write(timestamp +";"+ str(data1) +";"+ str(data1) +";"+ str(data3) +"\n")
    time.sleep(seconds)

the .csv-file should be filled with measuring data. The problem now is, that if I check the file after exiting the script, it's completely empty , not even the column identifiers are present. However, if I use a for-loop, it works like it should.

Very strange for my understanding.

I assume you want to leave this program running indefinitely to collect data from some kind of sensor, and so I suspect the issue is the default buffering from your open() call.

Firstly, you should almost always be using a "with" block like @Spirine suggests, but in your case a slight modification is in order:

with open("data/"+ name + "_" + timestamp + ".csv", "w", 1) as result:

The , 1 at the end indicates line buffering, meaning that Python will write the file to disk at the end of each line. Also, consider using str.format() to make it a bit more polished:

log_line_template = "{ts:%H:%M:%S};{d1};{d2};{d3}\n"
filename = "data/{n}_{ts:%H_%M_%S}.csv".format(n=name, ts=datetime.now())

with open(filename, "w", 1) as result:
    result.write("time; data1; data2; data3 \n")`

    while True:
        data1,data2,data3 = device.fetchData()
        result.write(log_line_template.format(
            ts=datetime.now(), data1, data2, data3
        ))
        time.sleep(seconds)

If your file isn't correctly written it's because you're program is incorrectly stopped: to escape of the while loop, what do you do ? If you don't want to modify your code too much, you could just use the open context manager:

with open("data/"+ name + "_" + timestamp + ".csv", "w") as result:
    result.write("time; data1; data2; data3 \n")`

    while True:
        timestamp = time.strftime("%H:%M:%S", time.localtime())
        data1,data2,data3 = device.fetchData()

        result.write(timestamp +";"+ str(data1) +";"+ str(data1) +";"+ str(data3) +"\n")
        time.sleep(seconds)

As it, no matter what happens, your file will be correctly closed in the end of your program.

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