Python Forum
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
need help
#1
Hello

i have ten line of results like

Count
0
0
0
0
0
0
0
0
0
0

this is the results of 10 lines and i want to run a loop to check if the value of my any line become 1 than my loop will run, so can anyone help me how can i do so
Reply
#2
Could you post you code so that it would be easier for us to help you? :)
Reply
#3
cmd1 = "tail -n 10 /home/anv/log_data/2020-01-23abc-itunesmonitor |grep -E 'guest.abc|itunes_dfvi' | awk '{print $1}' | awk -F, '{print $2'} | awk -F} '{print$1}'"

out1, err1 = subprocess.Popen(cmd1, shell = True, stdout = subprocess.PIPE, stderr = subprocess.PIPE).communicate()
out1 =out1[:-1].decode('utf-8').split("\n")
print ("Count", *out1, sep = "\n")
out put

Count
0
0
0
0
0
0
0
0
0
0
Reply
#4
Ok, as I understand it you want to check if the value you are receiving is 1 at any point and start a loop if that is the case.
Since you have a list you can use the any function. But since you are handling a list of string you would need to convert the 0's and 1's inside of the list into useable integer values. You could use the map function to do this. so to check if 1 exists inside of the list you could do the following:
if any(map(int, out1)):
    # start your loop
since 0 in bool expressions is interpreted as False and everything above 0 is considered True you can use that to determine if a 1 is present in the list.
To wrap it up in one sentence:
You convert every single string in the list out1 to integer and check if anything above zero could be find in it.
Reply


Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020