Issue
Code that im working with :
def METRICS():
if os.path.exists(""):
os.remove("")
link = ""
options = webdriver.ChromeOptions()
options.add_argument("--headless")
options.add_argument("--window-size=1920,1080")
prefs = {'download.default_directory' : ''}
options.add_experimental_option('prefs', prefs)
driver = webdriver.Chrome(options=options)
driver.get(link)
sleep(10)
all_avg = driver.find_elements_by_xpath('//*[@id="panel-4"]/div/div[1]/div/div[2]/div/plugin-component/panel-plugin-graph/grafana-panel/ng-transclude/div/div[2]/div/div[1]/div')
lst = []
for avg in all_avg:
#print(avg.text)
lst.append(avg.text)
#print(lst)
list = [x.replace('\n',',')for x in lst]
print (list)
OUTPUT :
['801,100.0%,802,78.3%,803,99.8%,804,18.5%,805,99.9%,811,100.0%,812,99.9%,813,97.9%,814,99.8%,815,99.9%,816,98.5%,817,100.0%,818,69.9%,819,100.0%,820,100.0%,821,95.9%,822,100.0%,823,100.0%']
I need the first set of numbers ie 801 802 803 etc to be in one list and the percentages% to be in another . this is so i can format in to a DF correctly.
Desired output :
list_1 = ['801',802',803',804', etc ]
list_2 = ['100%',78.3%,'99.8%','18.5%', etc ]
Final output after i use pandasDF and tabulate :
| Placehol | 701 | 702 | 703 | 704 | 705 | 706 | 707 | 708 | 709 | 710 | 711 | 712 | 713 |
|:-------------|:------|:------|:------|:------|:------|:------|:------|:------|:-------|:-------|:------|:------|:------|
| Availability | 99.1% | 98.5% | 67.0% | 87.0% | 98.3% | 98.0% | 96.9% | 99.1% | 100.0% | 100.0% | 95.9% | 98.1% | 98.1% |
Solution
You can use Python List Slicing: https://www.geeksforgeeks.org/python-list-slicing/
lst = [x.replace('\n',',') for x in lst] # ['801,100.0%,802,78.3%,803...']
# convert to a list of strings
result = lst.join(",").split(",") # ['801','100.0%','802','78.3%','803',...]
# start from the first element, add every 2n + 1 element to result_1
result_1 = result[::2]
# start from the second element, add every 2n element to result_2
result_2 = result[1::2]
Hmm, I have not noticed that your list
has just one element. Edited my code sample.
Answered By - Vlad Emelianov
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.