How to store python readings in a specific location

Asked 2 years ago, Updated 2 years ago, 15 views

This may be a rudimentary question, but it will not be solved, so I would appreciate it if you could let me know.
We would like to store the numbers read below in a specific location.
Suppose you get the results 1,2,3,4,5,6 from the six data specified in device_names1.
I'd like to store those numbers in places like A1_1,A1_2.....
In this case, is the array appropriate?

 if__name__=='__main__':
    device_names1 = ["CM700", "CM701", "CM702", "CM703", "CM704", "CM705" ]
    ip = '192.168.111.111'
    port = 1234

    for device_name in device_names1:
        A1 = dev(ip, port, device_name, 1)
        print(device_name,A1)

Additional questions
Thank you for your reply. The output is as above.
There was a missing explanation, so I will add it.
I have obtained information from another model by TCP/IP, and I would like to store it in a place like A1_1, A1_2.. and quote it in another place.
Retrieved values CM700:22, CM701:2, CM702:30, etc. *The retrieved values are random.

#get a value in 16 decimal bits
defrecv_10 deci 16 bit (ip, port, device_name, data_length):
    recv = ReceiveTruePLCValue (ip, port)
    recv_data = recv.receive_plc_data([device_name,data_length,
                                       '10decimal_16bit'])
    return recv_data

if__name__=='__main__':
    # Tested use
    device_names=["CM700", "CM701", "CM702", "CM703", "CM704", "CM705" ]
    ip = '192.168.111.111'
    port = 1234
    for device_name in device_names:
        A1 = recv_10 deci 16 bit (ip, port, device_name, 1)
        print(A1)

python

2022-09-29 22:11

1 Answers

Array is fine, but isn't the dictionary type appropriate?
The following code creates an array called dic_A and substitutes it for keys such as A1_1.

def dev(ip, port, device_name, i):
    return i+1

if__name__=='__main__':
    device_names1 = ["CM700", "CM701", "CM702", "CM703", "CM704", "CM705" ]
    ip = '192.168.111.111'
    port = 1234

    dic_A={}#Dictionary declaration
    for i, device_name in enumerate(device_names1):
        key="A1_"+str(i+1)
        value = dev(ip, port, device_name, i )
        dic_A[key] = value
        print(device_name,key,dic_A[key])#Each key and corresponding value
        
    print(dic_A)

Output Results

CM700A1_11
CM701 A1_22
CM702 A1_33
CM703 A1_44
CM704 A1_55
CM705 A1_66
{'A1_1':1,'A1_2':2,'A1_3':3,'A1_4':4,'A1_5':5,'A1_6':6}


2022-09-29 22:11

If you have any answers or tips


© 2024 OneMinuteCode. All rights reserved.