python list iteration with boolean - python-3.x

class_abbrevs_2 = [x.text for x in subject_page.find_all('td')[1].find_all('a')]
I am going through opening each link and then here is the page that is giving me the problem. It only has the one 'td', this has only a single td:
http://registrar.indiana.edu/browser/soc4168/CTIH/index.shtml
and here is what a normal page looks like that has 2 'td':
http://registrar.indiana.edu/browser/soc4168/CSCI/index.shtml
Can someone tell me how I can run this line only if it's true. I can't figure out the syntax. Just for a little background if needed I am webscraping with python and BS4. Lmk if there is any other info that would help. Thanks

If I understand correctly from the comment above, I think you just want this?
tds = subject_page.find_all('td')
if len(tds) > 1:
class_abbrevs_2 = [x.text for x in tds[1].find_all('a')]

You can simplify the code to a select using nth-of-type to look for a second td:
text = [a.text for a in soup.select("#crsebrowser td:nth-of-type(2) a")]
If there is no second td inside the table then it will find nothing so you with end up with a list of text from the anchors or nothing so you don't need any other logic to get the data.

Related

how to copy to clipboard text between h2 tags in selenium python

what i try to do here is get email code for verification. so I log in to the email, select and copy the 6 digits code from the email and paste it to the other tab. everything is done except i can not double click to select the 6 digit code and copy it to clipboard. the code is between h2 tag and nothing else, like this: 639094 where 639094 is actually the code which i need to be copied. how can i find the code element or whatever and copy it? here is a screen shot of the email and the chrome inspect element if anything helps.
this is the code that I use to copy the code:
codeID = driver.find_element(By.XPATH,
'//table[#class="main"]//tr//td//p//h2').text
ActionChains = ActionChains(driver)
ActionChains.double_click(codeID).perform()
time.sleep(2)
codeID.send_keys(Keys.CONTROL + 'c')
text = pyperclip.paste()
print(text)
screen shot
element is found however looks like can not be copied. the error is Element is not reachable by keyboard. if i do everything automatically up until the element is selected with double click and copy the element with my actual keyboard the element is copied, however when selenium try to copy i get the error from above. the code i use to double click the element is:
codeID = driver.find_element(By.XPATH, '//*[#id="message-htmlpart1"]/div/table/tbody/tr/td[2]/div/table/tbody/tr/td/table/tbody/tr/td/h2')
ActionChains = ActionChains(driver)
ActionChains.double_click(codeID).perform()
time.sleep(2)
and to do the copy is :
codeID.send_keys(Keys.CONTROL + 'c')
text = pyperclip.paste()
print(text)
this is the part where the error ocur:
codeID.send_keys(Keys.CONTROL + 'c')
text = pyperclip.paste()
print(text)
for some reason it says "Element is not reachable by keyboard" but the element/code numbers are selected.
if I use print(text) they are also printed in the console.
driver.find_element_by_xpath('//table[#class="main"]//tr//td//h2').text this will give you the text/code
Hey i will analyse this problem with you
For the first part :
try to take that XPath you have and past it in the Xpath helper (google chrome extension)
=> If you find that element , than the problem in your code
=> if you don’t than the element is already in a frame or in a table
The solution is to change your drive to the new frame and relocate the element inside the frame
Exemple :
iframe_xpath = driver.find_element_by_xpath('//iframe')
driver.switch_to.frame('iframe_xpath')
Now try to relocate the element starting from the iframe
For the second part :
You say it’s a table so you need to mention the /td[i] and /tr[j] value where the number is located so you can get it
Exemple
d = driver.find_element_by_xpath( "//tr[i]/td[j]").text
I hope that’s help

Use String text as Code in Visual Basic for Excel

For various reasons, I need to concatenate a text of the form [NAME].Value by changing the value of NAME to an inputbox entry.
Something like this:
Sub example()
data_in = InputBox("Give me something: ")
mystring1 = "[" & data_in & "].Value"
a = Evaluate(mystring1) 'I know this is wrong, but I don't know how to do so.
End Sub
I know it can be done in other ways, and the example in which I want to use this code is not exactly this one, and while it can be done in several ways here, in the original code it can only be done this way.
I want, based on the input in the imputbox, to concatenate the string in whatever way, and subsequently cast that string as code to store the value in another variable, to be used later in the code.
I am not able to get VBA to read the string text as code. I have seen that there is a way that consists of creating a macro from this first macro, execute it, and then delete the recently created macro. The problem with this solution is that doing that I can't save the variable when returning to the initial macro (I don't want to use global variables).
Surely there must be a way?
Thank you very much.
EDIT: The code above returns Error 2015
In order to use a string as if it was code, you can use the evaluate function (exists in most languages)
The official documentation mentions this example:
[a1].Value = 25
Evaluate("A1").Value = 25
trigVariable = [SIN(45)]
trigVariable = Evaluate("SIN(45)")
Set firstCellInSheet = Workbooks("BOOK1.XLS").Sheets(4).[A1]
Set firstCellInSheet = _
Workbooks("BOOK1.XLS").Sheets(4).Evaluate("A1")
I have figured out the easiest way to do it, sorry for posting the question so soon.
Thanks to #Andreas for the solution. I'll write it here in case than could be useful to someone.
Sub example()
data_in = InputBox("Give me something: ")
a = Range(data_in).Value
Debug.Print a
End Sub
In the end the simplest thing is the last thing you try...

Show all matches between two tables in excel

I need help to show all matches between two tables in excel. I've searched everywhere and didn't find any answer yet.
Please check the table on the image.
Table Sample:
Thank you.
Try this:
Set myNewRow = ActiveWorkbook.Worksheets(1).ListObject(1).ListRows.Add
FOR i = LBound(Table1) TO UBound(Table1)
FOR a = LBound(Table2) To UBound(Table2)
IF Table1(i).Value = Table2(a).Value THEN myNewRow(i) = Table2(a).Value
ENDIF
NEXT a
NEXT i
Where myNewRow is the result table. You are going to have to use the nested for loop to accomplish this.
You might want to try this (confirm with CTRLSHIFTENTER)
IFERROR(INDEX($B$2:$B$7,SMALL(IF($B$2:$B$7=TRANSPOSE($A$2:$A$3),ROW($C$1:$C$6)),ROW(A1))),"")

Issues with comparing Strings in Fortran?

I am trying to compare a string with a set of values read into a character array. But the comparison doesn't work the way it is expected to.I have attached the code below for the check.
PROGRAM MWE
CHARACTER(LEN=80) :: FILENAME,HEADER,REMARKS
CHARACTER*6,ALLOCATABLE :: RESNAME(:)
INTEGER :: NHEADER,NATOMS,I
ALLOCATE(RESNAME(10))
FILENAME = "./thy_thy_wat.psf"
OPEN(10,FILE=FILENAME,STATUS='OLD',FORM='FORMATTED')
READ(10,'(A4)') HEADER
READ(10,'(B10)')
READ(10,'(T6,I4)') NHEADER
DO I=1,NHEADER
READ(10,'(A50)') REMARKS
END DO
READ(10,'(B10)')
READ(10,'(T5,I4)') NATOMS
DO I=1,10
READ(10,'(T19,A5)') RESNAME(I)
END DO
WRITE(*,*) RESNAME
IF (RESNAME(1) == "THY") THEN
WRITE(*,*) "YES"
END IF
END PROGRAM MWE
I can visually check that the values match, but the code doesn't give that. Any help would be appreciated
https://drive.google.com/file/d/1WHOv65Lbb3PRSuE2YwMAZk1L-uqYDhXb/view?usp=sharing is the link to the file that is read in using the above code.
EDIT: I was able to solve the issue by playing around with the tabs. Thanks for the help everyone.

How to divide and get the percentage of the value of two different textboxes in userform?

i'm new with VBA and i'm currently working on creating an evaluation form for one of our LOBs. I'm having a hard time understanding what is wrong with my current code wherein it would not divide the value of two text boxes. when i tried to divide the value of one text box to a number it would work. however that is not what i'm looking for since the value in the text boxes are dependent on the form questions (there are questions that are NA).
I have tried the code below and its not working.
Sub CusImp2_div()
CusImp2.Value = Val(CusImp1.Value) / Val(CusImp3.Value)
CusImp2.Value = Format(Me.CusImp2.Value, "0.00%")
End Sub
the source of the values are...
Sub custotal_mulcal()
Me.CusImp1.Value = Val(CustTotal1.Value) * (0.33)
Me.CusImp1.Value = Application.WorksheetFunction.Round(Val(CusImp1.Value), 2)
Me.CusImp3.Value = Val(CustTotal2.Value) * (0.33)
Me.CusImp3.Value = Application.WorksheetFunction.Round(Val(CusImp3.Value), 2)
End Sub
when i run the formula and activate the userform i'm experiencing a debug problem and i can't seem to figure out what to do.
i expect the output to be a percentage of the output ex. 96%
i really hope that someone can help me with this and i'm actually frustrated because im runnning after a deadline.
thank you!
'I think I found the answer to this question. I found the formula below in Youtube and the form is working well now. The only limitation though is that the percentage would not change even though I tried to change my answers (I will look into how I can work around this one)..
Sub CusImp2_div()
Dim a As Double
Dim b As Double
Dim answer As Double
a = CusImp1.Value
b = CusImp3.Value
answer = a / b
If b <> 0 Then
answer = a / b
End If
Me.CusImp2.Value = answer
Me.CusImp2.Value = Format(Me.CusImp2.Value, "0.00%")
End Sub

Resources