Search code examples
pythonpython-3.9

Is it possible to inject a number into excel at every 1 minute interval using Python?


Currently, I have a code that updates an excel sheet every 5 seconds. It will print

0 400 800 800 800

1 400 800 800 800

2 400 800 800 800

What I am trying to figure out is how do I add in a random number into that list at every 1min interval, for example,

0 400 800 800 800

1 **425** 800 800 800

2 400 800 800 800

The code shown here is the current code I am having and was wondering if anyone can help me out here, thanks so much :).

import csv
import random
import time

x_value = 0
total_1 = 400 #voltage
total_2 = 800 #current(1)
total_3 = 800 #current(2)
total_4 = 800 #current(3)

fieldnames = ["Timestamp", "Voltage", "[L1] Current(1)", "[L1] Current(2)", "[L1] Current(3)"]

with open('test 4.0.csv', 'w') as csv_file:
    csv_writer = csv.DictWriter(csv_file, fieldnames = fieldnames)
    csv_writer.writeheader()
    
while True:
    with open('test 4.0.csv', 'a') as csv_file:
        csv_writer = csv.DictWriter(csv_file, fieldnames = fieldnames)
        
        info = {
            "Timestamp": x_value,
            "Voltage": total_1,
            "[L1] Current(1)": total_2,
            "[L1] Current(2)": total_3,
            "[L1] Current(3)": total_4
            }
        csv_writer.writerow(info)
        print(x_value, total_1, total_2, total_3, total_4)
        
        x_value += 1

    time.sleep(5) #controls the speed of the loop eg. 1 = 1sec

Solution

  • Yes, you can do that by adding a simple flag to indicate if the voltage value should be randomized.

    Before your loop, you may initialize a flag by

    ...
    total_4 = 800 #current(3)
    randomVoltageFlag = False # new flag
    ...
    

    Then in your loop, check if the flag is true and assign the value accordingly

    While True:
        ...
        info = {
           ...
           "Voltage": random.randrange(200,500) if randomVoltageFlag else total_1,
           ...
    

    Lastly, at the end of your loop, you can switch the flag to flip it for the next iteration

    randomVoltageFlag = not randomVoltageFlag
    

    The full modified code should now be

    import csv
    import random
    import time
    
    x_value = 0
    total_1 = 400 #voltage
    total_2 = 800 #current(1)
    total_3 = 800 #current(2)
    total_4 = 800 #current(3)
    randomVoltageFlag = False # new flag
    
    fieldnames = ["Timestamp", "Voltage", "[L1] Current(1)", "[L1] Current(2)", "[L1] Current(3)"]
    
    with open('test 4.0.csv', 'w') as csv_file:
        csv_writer = csv.DictWriter(csv_file, fieldnames = fieldnames)
        csv_writer.writeheader()
        
    while True:
        with open('test 4.0.csv', 'a') as csv_file:
            csv_writer = csv.DictWriter(csv_file, fieldnames = fieldnames)
            
            info = {
                "Timestamp": x_value,
                "Voltage": random.randrange(200,500) if randomVoltageFlag else total_1,
                "[L1] Current(1)": total_2,
                "[L1] Current(2)": total_3,
                "[L1] Current(3)": total_4
                }
            csv_writer.writerow(info)
            print(x_value, total_1, total_2, total_3, total_4)
            
            x_value += 1
            randomVoltageFlag = not randomVoltageFlag
        time.sleep(5) #controls the speed of the loop eg. 1 = 1sec
    

    Notes, this is just an example for you. It can be further optimized by replacing the flag with your x_value by utilizing the % mod operator, but I will leave that part for you to try.